Advertisement
MjnMixael

axui

Apr 30th, 2015
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 32.00 KB | None | 0 0
  1. #Conditional Hooks
  2. $Application: FS2_Open
  3.  
  4. $On Game Init:
  5. [
  6.  
  7. AXUI = {}
  8.  
  9. function AXUI:Init()
  10.  
  11. --Please don't mind the many commented debug lines!
  12. self:ScreenInit()
  13. self:SetupColors() --Creates a table of FreeSpace UI colors
  14.  
  15. if cf.fileExists("axui.cfg", "data/config", true) then
  16. self.Defaults = axemParse:ReadJSON("axui.cfg")
  17. else
  18. self.Defaults = {}
  19. end
  20.  
  21. self.Fonts = {}
  22. self.Fonts.Large = self.Defaults.LargeFont or gr.Fonts[2]
  23. self.Fonts.Medium = self.Defaults.MediumFont or gr.Fonts[3]
  24. self.Fonts.Small = self.Defaults.SmallFont or gr.Fonts[1]
  25. self.Fonts.Old = nil
  26.  
  27. self.MouseButton = {false, false, false}
  28.  
  29. end
  30.  
  31. function AXUI:ScreenInit()
  32.  
  33. self.Screen = {} --w, h, origin_x, origin_y
  34.  
  35. local baseX = 1024
  36. local baseY = 768
  37.  
  38. local unscaledScreenWidth = gr.getScreenWidth()
  39. local unscaledScreenHeight = gr.getScreenHeight()
  40. local aspectX = unscaledScreenWidth / baseX
  41. local aspectY = unscaledScreenHeight / baseY
  42.  
  43. --if true then
  44. if gr.isMenuStretched() or self.InGame then
  45. self.Screen.x_aspect = aspectX
  46. self.Screen.y_aspect = aspectY
  47. self.Screen.w = unscaledScreenWidth
  48. self.Screen.h = unscaledScreenHeight
  49. self.Screen.x = 0
  50. self.Screen.y = 0
  51. else
  52. local aspect = math.min(aspectX, aspectY)
  53. self.Screen.x_aspect = aspect
  54. self.Screen.y_aspect = aspect
  55. self.Screen.w = baseX * aspect
  56. self.Screen.h = baseY * aspect
  57. self.Screen.x = (unscaledScreenWidth - self.Screen.w) / 2
  58. self.Screen.y = (unscaledScreenHeight - self.Screen.h) / 2
  59. end
  60.  
  61. for k, v in pairs(self.Screen) do
  62. ba.print(k .. ": " .. v .. "\n")
  63. end
  64.  
  65. end
  66.  
  67. --Font Functions
  68.  
  69. function AXUI:SetupFont(t, dont_reset)
  70.  
  71. if t.Font then
  72. self:ChangeFont(t.Font)
  73. else
  74.  
  75. if t.Scaled then
  76. t.Scaled = true
  77. t.ScaleFont = true
  78. else
  79. t.Scaled = false
  80. t.ScaleFont = false
  81. end
  82.  
  83. if t.Scaled == "font" then
  84. t.Scaled = false
  85. t.ScaleFont = true
  86. end
  87.  
  88. self:AutoFontScale(t.ScaleFont)
  89.  
  90. end
  91.  
  92. t.FontHeight = gr.CurrentFont.Height
  93. if t.Text then
  94. t.TextWidth = gr.getStringWidth(t.Text)
  95. end
  96.  
  97. t.TextHeight = t.FontHeight
  98.  
  99. if dont_reset then
  100. self:ResetFont()
  101. end
  102.  
  103. end
  104.  
  105. function AXUI:AutoFontScale(enable)
  106.  
  107. if not enable then
  108. return
  109. end
  110.  
  111. if AXUI.Screen.h >= 850 then
  112. self.Fonts.Old = gr.CurrentFont
  113. gr.CurrentFont = self.Fonts.Medium
  114. else
  115. gr.CurrentFont = self.Fonts.Small
  116. end
  117.  
  118. end
  119.  
  120. function AXUI:ChangeFont(font)
  121.  
  122. self.Fonts.Old = gr.CurrentFont
  123. gr.CurrentFont = gr.Fonts[font]
  124.  
  125. end
  126.  
  127. function AXUI:ResetFont()
  128.  
  129. if self.Fonts.Old ~= nil then
  130. gr.CurrentFont = self.Fonts.Old
  131. self.Fonts.Old = nil
  132. end
  133.  
  134. end
  135.  
  136. function AXUI:ScaleCoords(x, y)
  137.  
  138. if x == nil then
  139. x = 0
  140. end
  141.  
  142. if y == nil then
  143. y = 0
  144. end
  145.  
  146. x = x * self.Screen.x_aspect
  147. y = y * self.Screen.y_aspect
  148.  
  149. return x, y
  150.  
  151. end
  152.  
  153. function AXUI:SetupColors()
  154.  
  155. --Values taken from the FreeSpace code, there's probably a better way to do this, but this works.
  156.  
  157. self.Color = {}
  158.  
  159. self.Color.blue = { 93, 93, 128, 255}
  160. self.Color.bright_blue = { 185, 185, 255, 255}
  161.  
  162. self.Color.green = { 0, 120, 0, 255}
  163. self.Color.bright_green = { 50, 190, 50, 255}
  164.  
  165. self.Color.invisible = {0, 0, 0, 0}
  166. self.Color.black = { 0, 0, 0, 255}
  167. self.Color.grey = { 65, 65, 65, 255}
  168. self.Color.silver = { 160, 160, 160, 255}
  169. self.Color.white = { 105, 105, 105, 255}
  170. self.Color.bright_white = { 255, 255, 255, 255}
  171.  
  172. self.Color.violet_gray = { 160, 144, 160, 255}
  173. self.Color.violet = { 192, 104, 192, 255}
  174.  
  175. self.Color.dim_red = { 80, 6, 6, 255}
  176. self.Color.red = { 126, 6, 6, 255}
  177. self.Color.bright_red = { 200, 0, 0, 255}
  178.  
  179. self.Color.pink = { 185, 150, 150, 255}
  180. self.Color.light_pink = { 230, 190, 190, 255}
  181.  
  182. self.Color.yellow = { 255, 255, 122, 255}
  183. self.Color.bright_yellow = { 255, 255, 0, 255}
  184.  
  185. self.Color.ui_light_green = { 161, 184, 161, 255}
  186. self.Color.ui_green = { 190, 228, 190, 255}
  187.  
  188. self.Color.ui_light_pink = { 184, 161, 161, 255}
  189. self.Color.ui_pink = { 228, 190, 190, 255}
  190.  
  191. end
  192.  
  193. function AXUI:SetColor(c)
  194.  
  195. --Basically gr.setColor with predefined colors so I don't need to keep remembering them
  196.  
  197. if c == nil then
  198. return
  199. end
  200.  
  201. local t = self.Color[c]
  202.  
  203. if t ~= nil then
  204. gr.setColor(t[1], t[2], t[3], t[4])
  205. end
  206.  
  207. end
  208. --Create Element Functions
  209. function AXUI:CreateButton(text, x, y, text_x, text_y, color, color_h, color_c, bitmap, bmp_x, bmp_y, bitmap_h, bitmap_c, scaled, level, font)
  210.  
  211. --This will return a table with the necessary values that enable the use of a button
  212. --Most fields self explanitory, text is text describing the button, color is text's color, bitmap is the button itself
  213. --x, y are position
  214. --text_x/y and bmp_x/y are offsets
  215. --_h = hover color / bitmap
  216. --_c = clicked color / bitmap
  217. --scaled = if it needs to be scaled as the interface does
  218. --level = What focus level this button may be pressed. Use AXUI:SetFocus(level) to set focus level
  219.  
  220. local t = {}
  221.  
  222. t.X = x
  223. t.Y = y
  224.  
  225. t.Clickable = true --Can we click it?
  226. t.Hoverable = true --Can we hover of it?
  227. t.Active = false --Active = Clicked bitmap used all the time
  228.  
  229. t.Draw = self.Draw --Button Drawing Function
  230. t.Clicked = self.Clicked --Notification if mouse clicks this
  231. t.CalculateRegion = self.CalculateRegion --Recalculate Region if we mess around with its coordinates later
  232.  
  233. t.Hovered = false --We're not hovering... yet
  234. t.Scaled = scaled
  235.  
  236. if text and text_x and text_y then
  237. t.Text = text
  238. t.TextX = t.X + text_x
  239. t.TextY = t.Y + text_y
  240. end
  241.  
  242. t.Offset = 0 --Buttons don't use offset, but it might be expected
  243. t.Lines = 1
  244.  
  245. if font then
  246. t.Font = font
  247. end
  248.  
  249. AXUI:SetupFont(t)
  250.  
  251. t.Level = level
  252.  
  253. t.Color = color
  254. t.Color_H = color_h
  255. t.Color_C = color_c
  256.  
  257. t.Bitmap = bitmap --Standard Button
  258. t.Bitmap_H = bitmap_h --Highlighted Button (mouse over)
  259. t.Bitmap_C = bitmap_c --Clicked Button (mouse click)
  260.  
  261. if bmp_x ~= nil and bmp_y ~= nil then
  262. t.BitmapX = t.X + bmp_x
  263. t.BitmapY = t.Y + bmp_y
  264. end
  265.  
  266. --Only one of these is actually required...
  267. if bitmap ~= nil then
  268. t.BitmapWidth = gr.getImageWidth(bitmap)
  269. t.BitmapHeight = gr.getImageHeight(bitmap)
  270. elseif bitmap_h ~= nil then
  271. t.BitmapWidth = gr.getImageWidth(bitmap_h)
  272. t.BitmapHeight = gr.getImageHeight(bitmap_h)
  273. elseif bitmap_c ~= nil then
  274. t.BitmapWidth = gr.getImageWidth(bitmap_c)
  275. t.BitmapHeight = gr.getImageHeight(bitmap_c)
  276. end
  277.  
  278. if scaled == true then --Scale... if we must...
  279.  
  280. t.TextX, t.TextY = self:ScaleCoords(t.TextX, t.TextY)
  281. t.TextWidth, t.TextHeight = self:ScaleCoords(t.TextWidth, t.TextHeight)
  282.  
  283. t.BitmapX, t.BitmapY = self:ScaleCoords(t.BitmapX, t.BitmapY)
  284. t.BitmapWidth, t.BitmapHeight = self:ScaleCoords(t.BitmapWidth, t.BitmapHeight)
  285.  
  286. end
  287.  
  288. if text ~= nil and text_x ~= nil and text_y ~= nil then
  289. t.TextX = t.TextX + AXUI.Screen.x
  290. t.TextY = t.TextY + AXUI.Screen.y
  291. end
  292.  
  293. if bmp_x ~= nil and bmp_y ~= nil then
  294. t.BitmapX = t.BitmapX + AXUI.Screen.x
  295. t.BitmapY = t.BitmapY + AXUI.Screen.y
  296. end
  297.  
  298. t:CalculateRegion()
  299.  
  300. return t
  301.  
  302. end
  303.  
  304. function AXUI:CreateDButton(host, text, text_x, text_y, color, color_h, color_c, bitmap, bmp_x, bmp_y, bitmap_h, bitmap_c, scaled, level, font)
  305.  
  306. local t = {}
  307.  
  308. t.Clickable = true --Can we click it?
  309. t.Hoverable = true --Can we hover of it?
  310. t.Active = false --Active = Clicked bitmap used all the time
  311.  
  312. t.Draw = self.Draw
  313. t.DDraw = self.DDraw --Button Drawing Function
  314. t.Clicked = self.Clicked --Notification if mouse clicks this
  315. t.CalculateRegion = self.CalculateRegion --Recalculate Region if we mess around with its coordinates later
  316.  
  317. t.Hovered = false --We're not hovering... yet
  318.  
  319. if mn.getObjectFromSignature(host):isValid() then
  320. t.Host = host
  321. end
  322.  
  323. t.Text = text
  324. t.TextX_0 = text_x
  325. t.TextY_0 = text_y
  326.  
  327. t.Offset = 0 --Buttons don't use offset, but it might be expected
  328. t.Lines = 1
  329.  
  330. t.Scaled = scaled
  331.  
  332. t.Level = level
  333.  
  334. if font then
  335. t.Font = font
  336. end
  337.  
  338. AXUI:SetupFont(t)
  339.  
  340. t.Color = color
  341. t.Color_H = color_h
  342. t.Color_C = color_c
  343.  
  344. t.Bitmap = bitmap --Standard Button
  345. t.Bitmap_H = bitmap_h --Highlighted Button (mouse over)
  346. t.Bitmap_C = bitmap_c --Clicked Button (mouse click)
  347.  
  348. t.BitmapX = bmp_x
  349. t.BitmapY = bmp_y
  350.  
  351. --Only one of these is actually required...
  352. if bitmap ~= nil then
  353. t.BitmapWidth = gr.getImageWidth(bitmap)
  354. t.BitmapHeight = gr.getImageHeight(bitmap)
  355. elseif bitmap_h ~= nil then
  356. t.BitmapWidth = gr.getImageWidth(bitmap_h)
  357. t.BitmapHeight = gr.getImageHeight(bitmap_h)
  358. elseif bitmap_c ~= nil then
  359. t.BitmapWidth = gr.getImageWidth(bitmap_c)
  360. t.BitmapHeight = gr.getImageHeight(bitmap_c)
  361. end
  362.  
  363. if scaled == true then --Scale... if we must...
  364.  
  365. t.TextX, t.TextY = self:ScaleCoords(t.TextX, t.TextY)
  366. t.TextWidth, t.TextHeight = self:ScaleCoords(t.TextWidth, t.TextHeight)
  367.  
  368. t.BitmapX, t.BitmapY = self:ScaleCoords(t.BitmapX, t.BitmapY)
  369. t.BitmapWidth, t.BitmapHeight = self:ScaleCoords(t.BitmapWidth, t.BitmapHeight)
  370.  
  371. end
  372.  
  373. t:CalculateRegion()
  374.  
  375. return t
  376.  
  377. end
  378.  
  379. function AXUI:CreateTextBox(text, text_x, text_y, width, height, color, scaled, font)
  380.  
  381. -- This creates a large scrollable text box for LOTS O' WORDS.
  382. -- x, y are where it starts, goes for width wide by height high
  383. -- text is colored as color, and scaled only affects the coordinate placing, not the text
  384. -- text does not need to be valid at first, nil is fine if you need it blank
  385.  
  386. t = {}
  387.  
  388. t.Clickable = false
  389. t.Hoverable = false
  390.  
  391. t.Draw = self.Draw --Draws the text
  392. t.ScrollText = self.ScrollText --Scrolls the text
  393. t.ChangeText = self.ChangeText --Changes the text contents, text can be nil initially
  394.  
  395. t.Text = text
  396. t.TextX = text_x
  397. t.TextY = text_y
  398.  
  399. t.Clipping = true
  400. t.Scaled = scaled
  401.  
  402. if font then
  403. t.Font = font
  404. end
  405.  
  406. AXUI:SetupFont(t)
  407.  
  408. t.TextWidth = width
  409. t.TextHeight = height
  410.  
  411. t.Color = color
  412. t.Offset = 0
  413. t.Lines = 0
  414.  
  415. if scaled == true then
  416.  
  417. t.TextX, t.TextY = self:ScaleCoords(t.TextX, t.TextY)
  418. t.TextWidth, t.TextHeight = self:ScaleCoords(t.TextWidth, t.TextHeight)
  419.  
  420. end
  421.  
  422. t.TextX = t.TextX + AXUI.Screen.x
  423. t.TextY = t.TextY + AXUI.Screen.y
  424.  
  425. return t
  426.  
  427. end
  428.  
  429. function AXUI:CreateListBox(list, list_x, list_y, list_width, list_height, color, color_h, color_c, scaled, level, font)
  430.  
  431. --Automated system that creates a series of text-only buttons
  432.  
  433. local t = {}
  434.  
  435. t.DrawList = self.DrawList
  436. t.ChangeList = self.ChangeList
  437. t.UpdateList = self.UpdateList
  438. t.ScrollList = self.ScrollList
  439.  
  440. t.List = list
  441. t.ListX = list_x
  442. t.ListY = list_y
  443. t.ListWidth = list_width
  444. t.ListHeight = list_height
  445. t.Color = color
  446. t.Color_H = color_h
  447. t.Color_C = color_c
  448.  
  449. t.Level = level
  450.  
  451. t.SelectedItem = 1
  452.  
  453. t.Scaled = scaled
  454.  
  455. if font then
  456. t.Font = font
  457. end
  458.  
  459. --if t.Text then
  460. AXUI:SetupFont(t)
  461. --end
  462.  
  463. if scaled == true then
  464.  
  465. t.ListX, t.ListY = self:ScaleCoords(t.ListX, t.ListY)
  466. t.ListWidth, t.ListHeight = self:ScaleCoords(t.ListWidth, t.ListHeight)
  467.  
  468. end
  469.  
  470. ba.print("LIST HEIGHT :" .. t.ListHeight .. "\n")
  471. ba.print("FONT HEIGHT :" .. t.FontHeight .. "\n")
  472. t.MaxLines = math.floor(t.ListHeight / (t.FontHeight + 3))
  473. t.ItemOffset = 0
  474.  
  475. --t.Scaled = scaled
  476.  
  477. --t.ListX = t.ListX + AXUI.Screen.x
  478. --t.ListY = t.ListY + AXUI.Screen.y
  479.  
  480. return t
  481.  
  482. end
  483.  
  484. function AXUI:CreateImageBox(bitmap, bmp_x, bmp_y, text, color, width, height, center_h, center_v, scaled, font, monochrome)
  485.  
  486. t = {}
  487.  
  488. ba.print("Starting to create image box!\n")
  489.  
  490. t.Draw = self.Draw
  491. t.ChangeText = self.ChangeText
  492. t.ChangeImage = self.ChangeImage
  493.  
  494. t.Bitmap = bitmap
  495. t.BitmapX_0 = bmp_x
  496. t.BitmapY_0 = bmp_y
  497. ba.print("1. t.BitmapX_0 = " .. t.BitmapX_0 .. "\n")
  498.  
  499. t.Text = text
  500. t.Color = color
  501. t.Monochrome = monochrome
  502.  
  503. t.TargetWidth_0 = width
  504. t.TargetHeight_0 = height
  505.  
  506. t.Center_H = center_h
  507. t.Center_V = center_v
  508.  
  509. t.Offset = 0
  510.  
  511. t.Scaled = scaled
  512.  
  513. if font then
  514. t.Font = font
  515. end
  516.  
  517. AXUI:SetupFont(t)
  518.  
  519.  
  520. t.BitmapX_0 = t.BitmapX_0 + AXUI.Screen.x
  521. ba.print("2. t.BitmapX_0 = " .. t.BitmapX_0 .. "\n")
  522. t.BitmapY_0 = t.BitmapY_0 + AXUI.Screen.y
  523.  
  524. if t.Bitmap ~= nil then
  525. ba.print("Valid bitmap found!\n")
  526. t:ChangeImage(t.Bitmap)
  527. end
  528.  
  529. return t
  530.  
  531. end
  532.  
  533. function AXUI:CreateThumbnails(tt, x, y, width, height, th_w, th_h, pad_h, pad_v, scaled, level, font)
  534.  
  535. --Arrange a bunch of buttons in a grid, starting at x, y, being width x height, keeping padding_h px between them
  536.  
  537. local t = {}
  538.  
  539. t.PlaceThumbnails = self.PlaceThumbnails
  540. t.DrawThumbnails = self.DrawThumbnails
  541. t.ChangeThumbnailList = self.ChangeThumbnailList
  542. t.ScrollThumbnails = self.ScrollThumbnails
  543.  
  544. t.Items = tt
  545. t.X = x
  546. t.Y = y
  547. t.Width = width
  548. t.Height = height
  549. t.TWidth = th_w
  550. t.THeight = th_h
  551. t.PaddingH = pad_h
  552. t.PaddingV = pad_v
  553.  
  554. t.Scaled = scaled
  555.  
  556. if font then
  557. t.Font = font
  558. end
  559.  
  560. if t.Text then
  561. AXUI:SetupFont(t)
  562. end
  563.  
  564. t.RowOffset = 0
  565.  
  566. t.Level = level
  567.  
  568. if scaled == true then
  569.  
  570. t.X, t.Y = AXUI:ScaleCoords(t.X, t.Y)
  571. t.Width, t.Height = AXUI:ScaleCoords(t.Width, t.Height)
  572. t.TWidth, t.THeight = AXUI:ScaleCoords(t.TWidth, t.THeight)
  573. t.PaddingH, t.PaddingV = AXUI:ScaleCoords(t.PaddingH, t.PaddingV)
  574.  
  575. end
  576.  
  577. t.X = t.X + AXUI.Screen.x
  578. t.Y = t.Y + AXUI.Screen.y
  579.  
  580. t.ThumbnailsAcross = math.floor( t.Width / ((1 * t.PaddingH) + (t.TWidth) ))
  581. t.ThumbnailsHigh = math.floor( t.Height / ((1 * t.PaddingV) + (t.THeight) ))
  582. t.Offset = 0
  583.  
  584. if t.Items then
  585. t:PlaceThumbnails()
  586. end
  587.  
  588. return t
  589.  
  590. end
  591.  
  592. function AXUI:CalculateRegion(clipping)
  593.  
  594. --BUTTON FUNCTION ONLY
  595. --Calculates a bounding box for a bitmap button, text button, or hybrid text+bitmap button
  596. --Debug print commands below list where region has been calculated too
  597.  
  598. local clipX = 0
  599. local clipY = 0
  600.  
  601. if clipping then
  602. clipX = AXUI.Screen.x
  603. clipY = AXUI.Screen.y
  604. end
  605.  
  606. self.Region = {}
  607.  
  608. if self.Host then
  609.  
  610. local thisObject = mn.getObjectFromSignature(self.Host)
  611. self.Region.x1, self.Region.y1, self.Region.x2, self.Region.y2 = gr.drawTargetingBrackets(thisObject, false, 10)
  612. gr.setLineWidth(1)
  613.  
  614. else
  615. if self.Text and self.BitmapX then
  616. self.Region.x1 = math.min(self.TextX,self.BitmapX) + clipX
  617. self.Region.x2 = math.max(self.TextX + self.TextWidth, self.BitmapX + self.BitmapWidth) + clipX
  618. self.Region.y1 = math.min(self.TextY,self.BitmapY) + clipY
  619. self.Region.y2 = math.max(self.TextY + self.TextHeight, self.BitmapY + self.BitmapHeight) + clipY
  620. --ba.print("Text + Bitmap Region Made\n")
  621. elseif self.Text then
  622. self.Region.x1 = self.TextX + clipX
  623. self.Region.x2 = self.TextX + self.TextWidth + clipX
  624. self.Region.y1 = self.TextY + clipY
  625. self.Region.y2 = self.TextY + self.TextHeight + clipY
  626. --ba.print("Text Region Made\n")
  627. --ba.print("Text Coords: " .. math.floor(self.TextX) .. ", " .. math.floor(self.TextY) .. "\n")
  628. --ba.print("Text W/H: " .. math.floor(self.TextWidth) .. ", " .. math.floor(self.TextHeight) .. "\n")
  629. elseif self.BitmapX then
  630. self.Region.x1 = self.BitmapX + clipX
  631. self.Region.x2 = self.BitmapX + self.BitmapWidth + clipX
  632. self.Region.y1 = self.BitmapY + clipY
  633. self.Region.y2 = self.BitmapY + self.BitmapHeight + clipY
  634. --ba.print("Bitmap Region Made\n")
  635. --ba.print("X1: " .. math.floor(self.Region.x1) .. ", Y1:" .. math.floor(self.Region.y1) .. "\n")
  636. --ba.print("X2: " .. math.floor(self.Region.x2) .. ", Y2:" .. math.floor(self.Region.y2) .. "\n")
  637. else
  638. self.Region = nil
  639. ba.print("Problem making Region\n")
  640. end
  641. end
  642. end
  643. --Change Element Functions
  644. function AXUI:ChangeText(text)
  645.  
  646. --TEXTBOX FUNCTION ONLY
  647. --Pretty simple anyway
  648.  
  649. self.Text = text
  650.  
  651. end
  652.  
  653. function AXUI:ChangeImage(bitmap)
  654.  
  655. self.Bitmap = bitmap
  656.  
  657. if bitmap ~= nil then
  658.  
  659. ba.print("Changing bitmap!\n")
  660.  
  661. self.BitmapWidth = gr.getImageWidth(bitmap)
  662. self.BitmapHeight = gr.getImageHeight(bitmap)
  663.  
  664. if self.Scaled == true then --Scale... if we must...
  665. self.BitmapX, self.BitmapY = AXUI:ScaleCoords(self.BitmapX_0, self.BitmapY_0)
  666. self.TargetWidth, self.TargetHeight = AXUI:ScaleCoords(self.TargetWidth_0, self.TargetHeight_0)
  667. else
  668. self.BitmapX = self.BitmapX_0
  669. self.BitmapY = self.BitmapY_0
  670. self.TargetWidth = self.TargetWidth_0
  671. self.TargetHeight = self.TargetHeight_0
  672. end
  673.  
  674. if self.BitmapWidth > self.TargetWidth or self.BitmapHeight > self.TargetHeight then
  675. ba.print("We need to scale\n")
  676. local scalingFactor = 1
  677.  
  678. if (self.TargetWidth / self.BitmapWidth) < (self.TargetHeight / self.BitmapHeight) then
  679. scalingFactor = self.TargetWidth / self.BitmapWidth
  680. else
  681. scalingFactor = self.TargetHeight / self.BitmapHeight
  682. end
  683.  
  684. self.BitmapWidth = self.BitmapWidth * scalingFactor
  685. self.BitmapHeight = self.BitmapHeight * scalingFactor
  686. else
  687. ba.print("Didn't need to scale\n")
  688. end
  689.  
  690. if self.Center_H == true then
  691. self.BitmapX = self.BitmapX_0 + (self.TargetWidth / 2) - (self.BitmapWidth / 2)
  692. else
  693. self.BitmapX = self.BitmapX
  694. end
  695.  
  696. if self.Center_V == true then
  697. self.BitmapY = self.BitmapY_0 + (self.TargetHeight / 2) - (self.BitmapHeight / 2)
  698. else
  699. self.BitmapY = self.BitmapY
  700. end
  701.  
  702. if self.Text ~= nil then
  703.  
  704. ba.print("There is text!\n")
  705.  
  706. AXUI:AutoFontScale(self.Scaled)
  707.  
  708. self.TextX = self.BitmapX + (self.TargetWidth / 2) - (gr.getStringWidth(self.Text) / 2)
  709. if self.Scaled == true then
  710. self.TextX = self.BitmapX_0
  711. else
  712. self.TextX = self.BitmapX_0
  713. end
  714. self.TextY = AXUI.Screen.y + self.BitmapY + self.BitmapHeight + 25
  715. self.TextWidth = self.TargetWidth
  716. self.TextHeight = self.BitmapY + self.TargetHeight - self.TextY
  717.  
  718. ba.print("TEXT HEIGHT: " .. self.TextHeight .. "\n")
  719.  
  720. AXUI:ResetFont()
  721. else
  722. ba.print("I can't find text!\n")
  723. end
  724.  
  725. end
  726.  
  727. if self.Bitmap ~= nil then
  728.  
  729. ba.print(self.Bitmap .. "\n")
  730. ba.print("BitmapX: " .. self.BitmapX .. "\n")
  731. ba.print("BitmapY: " .. self.BitmapY .. "\n")
  732. ba.print("BitmapWIDTH: " .. self.BitmapWidth .. "\n")
  733. ba.print("BitmapHEIGHT: " .. self.BitmapHeight .. "\n")
  734. else
  735. ba.print("WHAT!\n")
  736. end
  737.  
  738. end
  739.  
  740. function AXUI:PlaceThumbnails()
  741.  
  742. local btnX = 0
  743. local btnY = 0
  744. local rowNum = 0
  745.  
  746. self.Buttons = {}
  747.  
  748. for i = 1, #self.Items - self.Offset do
  749.  
  750. btnX = self.X + (((i-1) % self.ThumbnailsAcross) * (self.TWidth + self.PaddingH))
  751. rowNum = math.floor((i-1) / self.ThumbnailsAcross)
  752. btnY = self.Y + (rowNum * (self.THeight + self.PaddingV))
  753.  
  754. if rowNum < self.ThumbnailsHigh then
  755.  
  756. --Create button, throw everything at 0,0 for now since it WILL be changing later
  757. self.Buttons[i] = AXUI:CreateButton(self.Items[i+self.Offset].Title, 0, 0, 0, 0, "white", "blue", "light_blue", self.Items[i+self.Offset].Image, 0, 0, nil, nil, false, self.Level, self.Font)
  758.  
  759. local thisButton = self.Buttons[i]
  760.  
  761. --Resize button
  762. if thisButton.BitmapWidth > self.TWidth or thisButton.BitmapHeight > self.THeight then
  763.  
  764. --ba.print("********************\n")
  765. ba.print(self.Items[i+self.Offset].Title .. " needs resizing!\n")
  766.  
  767. local scalingFactor = 1
  768.  
  769. if (self.TWidth / thisButton.BitmapWidth) < (self.THeight / thisButton.BitmapHeight) then
  770. ba.print("It is wider\n")
  771. scalingFactor = self.TWidth / thisButton.BitmapWidth
  772. else
  773. ba.print("It is taller\n")
  774. scalingFactor = self.THeight / thisButton.BitmapHeight
  775. end
  776.  
  777. --ba.print("scaling factor = " .. math.floor(scalingFactor*100) .. "%\n")
  778.  
  779. thisButton.BitmapWidth = thisButton.BitmapWidth * scalingFactor
  780. thisButton.BitmapHeight = thisButton.BitmapHeight * scalingFactor
  781.  
  782. --ba.print("Width/Height = " .. math.floor(thisButton.BitmapWidth) .. ", " .. math.floor(thisButton.BitmapHeight) .. "\n")
  783.  
  784. else
  785. ba.print("Didn't need to scale\n")
  786. end
  787.  
  788. thisButton.BitmapX = btnX + (self.TWidth/2) - (thisButton.BitmapWidth/2)
  789. thisButton.BitmapY = btnY + (self.THeight/2) - (thisButton.BitmapHeight/2)
  790.  
  791. AXUI:SetupFont(self, true)
  792.  
  793. if thisButton.Text ~= nil then
  794. thisButton.TextX = btnX + (self.TWidth /2) - (gr.getStringWidth(thisButton.Text)/2)
  795. thisButton.TextY = btnY + self.THeight + 5
  796. end
  797.  
  798. --Recalculate region since we probably screwed with the dimensions
  799. self.Buttons[i]:CalculateRegion()
  800.  
  801. AXUI:ResetFont()
  802.  
  803. --ba.print("Button " .. i .. " was made for thumbnails\n")
  804.  
  805. end
  806.  
  807. end
  808.  
  809. end
  810.  
  811. function AXUI:ChangeThumbnailList(t)
  812.  
  813. self.Items = t
  814. self:PlaceThumbnails()
  815.  
  816. end
  817. --Scroll Element Functions
  818. function AXUI:ScrollThumbnails(offset)
  819.  
  820. --first logical check, we shouldn't be able to scroll up when we're at the top
  821. --second logical check, we shouldn't be able to scroll past the last image
  822. --third logical check, we shouldn't be able to srcoll at all if there's not enough images
  823.  
  824. if (self.RowOffset + offset) < 0 or (self.RowOffset + offset + 1) >= (#self.Items / self.ThumbnailsAcross) or #self.Items <= (self.ThumbnailsAcross * self.ThumbnailsHigh) then
  825. return false
  826. else
  827. self.RowOffset = self.RowOffset+offset
  828. self.Offset = self.RowOffset * self.ThumbnailsAcross
  829. self:PlaceThumbnails()
  830. return true
  831. end
  832.  
  833. end
  834.  
  835. function AXUI:UpdateList(offset)
  836.  
  837. if self.List ~= nil then
  838.  
  839. --ba.print("List is ok, checking if list is smaller than max lines\nMax Lines: " .. self.MaxLines .. "\n")
  840.  
  841. if #self.List < self.MaxLines then
  842. --ba.print("List size is smaller than max lines\n")
  843. self.ItemLines = #self.List
  844. --ba.print("Item lines set to list size\n")
  845. else
  846. self.ItemLines = self.MaxLines
  847. --ba.print("List size is larger than max lines\n")
  848. end
  849.  
  850. --ba.print("About to intialize item set\n")
  851. self.Item = {}
  852. self.Group = AXUI:CreateRadioGroup()
  853. --ba.print("Item set intialized\n")
  854.  
  855. --ba.print("Offset: " .. offset .. " ItemLines: " .. self.ItemLines .. "\n")
  856.  
  857. for i = 1, self.ItemLines do
  858. --ba.print("Going to make a button " .. i .. "\n")
  859. self.Item[i] = AXUI:CreateButton(self.List[i + offset], self.ListX, (self.ListY + (i)*self.FontHeight), 0, 0, self.Color, self.Color_H, self.Color_C, nil, nil, nil, nil, nil, "font", self.Level, self.Font)
  860.  
  861. self.Group:AddToGroup(self.Item[i])
  862.  
  863. --ba.print("Coords:" .. self.ListX .. ", " .. (self.ListY + (i)*self.FontHeight) .. "\n")
  864. --ba.print("Made button " .. i .. "\n")
  865. end
  866.  
  867. --ba.print("Done making buttons\n")
  868.  
  869. end
  870.  
  871. end
  872.  
  873. function AXUI:ScrollList(offset)
  874.  
  875. local success = false
  876.  
  877. if #self.List > self.MaxLines then
  878.  
  879. self.ItemOffset = self.ItemOffset + offset
  880.  
  881. if self.ItemOffset < 0 then
  882. self.ItemOffset = 0
  883. elseif self.ItemOffset > (#self.List - self.MaxLines) then
  884. self.ItemOffset = #self.List - self.MaxLines
  885. else
  886. success = true
  887. self:UpdateList(self.ItemOffset)
  888.  
  889. local relativeSelectedPosition = self.SelectedItem - self.ItemOffset
  890.  
  891. if relativeSelectedPosition > 0 and relativeSelectedPosition <= self.MaxLines then
  892. self.Group:SwitchTo(self.SelectedItem - self.ItemOffset)
  893. end
  894. end
  895. end
  896.  
  897. return success
  898.  
  899. end
  900.  
  901. function AXUI:ChangeList(list)
  902.  
  903. ba.print("List was changed!\n")
  904. self.List = list
  905. self:UpdateList(0)
  906. ba.print("Finished changing list!\n")
  907.  
  908. end
  909.  
  910. function AXUI:ScrollText(x)
  911.  
  912. AXUI:AutoFontScale(scaling)
  913.  
  914. local textBlockHeight = (self.Lines + 1 ) * self.FontHeight
  915.  
  916. local success = false
  917.  
  918. if (textBlockHeight > self.TextHeight and self.Offset > (self.TextHeight - textBlockHeight)) or x > 0 then
  919. self.Offset = self.Offset + x
  920. success = true
  921. end
  922.  
  923. if self.Offset > 0 then
  924. self.Offset = 0
  925. success = false
  926. end
  927.  
  928. AXUI:ResetFont()
  929.  
  930. return success
  931.  
  932.  
  933. end
  934.  
  935. --Radio Group Functions
  936. function AXUI:CreateRadioGroup()
  937.  
  938. --Radio Groups tie together different buttons so that only one may become active at a time
  939.  
  940. t = {}
  941.  
  942. t.SwitchTo = self.SwitchTo
  943. t.AddToGroup = self.AddToGroup
  944.  
  945. return t
  946.  
  947. end
  948.  
  949. function AXUI:AddToGroup(item)
  950.  
  951. --RADIO GROUP FUNCTION ONLY
  952. --Adds a BUTTON to the radio group table
  953.  
  954. local newIndex = #self+1
  955.  
  956. self[newIndex] = item
  957.  
  958. return newIndex
  959.  
  960.  
  961. end
  962.  
  963. function AXUI:SwitchTo(number)
  964.  
  965. --RADIO GROUP FUNCTION ONLY
  966. --Sets the selected radio group index to be active (and not clickable)
  967.  
  968. for i = 1, #self do
  969. self[i].Active = false
  970. self[i].Clickable = true
  971. end
  972.  
  973. if number ~= nil then
  974. self[number].Active = true
  975. self[number].Clickable = false
  976. end
  977.  
  978. end
  979.  
  980. --Drawing Functions
  981. function AXUI:Draw()
  982.  
  983. --Giant generic drawing function. Looks for a bunch of things common to the different AXUI elements and does its best to draw them if they are valid.
  984.  
  985. local color = self.Color
  986. local bitmap = self.Bitmap
  987. local hovering = false
  988.  
  989. --If a button is hoverable, has a good region and we're at his focus level we will DO STUFF to it
  990. if self.Hoverable == true and self.Region ~= nil and self.Level == AXUI.Focus then
  991. if AXUI:IsMouseOver(self.Region) then
  992.  
  993. hovering = true
  994.  
  995. --gr.drawRectangle(self.Region.x1,self.Region.y1,self.Region.x2,self.Region.y2) --For checking hotspots
  996.  
  997. --Is this good for clicking?
  998. if io.isMouseButtonDown(MOUSE_LEFT_BUTTON) and self.Clickable == true then
  999.  
  1000. if self.Color_C ~= nil then
  1001. color = self.Color_C
  1002. end
  1003.  
  1004. if self.Bitmap_C ~= nil then
  1005. bitmap = self.Bitmap_C
  1006. elseif self.Bitmap ~= nil then
  1007. AXUI:SetColor("bright_blue")
  1008. --Draw a rectangle under the image to indicate it being highlighted
  1009. gr.setLineWidth(2)
  1010. gr.drawRectangle(self.BitmapX-2, self.BitmapY-2, self.BitmapX + self.BitmapWidth+2, self.BitmapY + self.BitmapHeight+2, false)
  1011. gr.setLineWidth(1)
  1012. end
  1013.  
  1014. else --Or are we just hovering over it
  1015.  
  1016. if self.Color_H ~= nil then
  1017. color = self.Color_H
  1018. end
  1019.  
  1020. if self.Bitmap_H ~= nil then
  1021. bitmap = self.Bitmap_H
  1022. elseif self.Bitmap ~= nil then
  1023. AXUI:SetColor("blue")
  1024. --Draw a rectangle under the image to indicate it being highlighted
  1025. gr.setLineWidth(2)
  1026. gr.drawRectangle(self.BitmapX-2, self.BitmapY-2, self.BitmapX + self.BitmapWidth+2, self.BitmapY + self.BitmapHeight+2, false)
  1027. gr.setLineWidth(1)
  1028. end
  1029.  
  1030. if self.Hovered == false then
  1031. self.Hovered = true
  1032. ad.playInterfaceSound(17)
  1033. end
  1034.  
  1035. end
  1036.  
  1037. else
  1038.  
  1039. if self.Hovered == true then
  1040. self.Hovered = false
  1041. end
  1042.  
  1043. end
  1044. end
  1045.  
  1046. --Active buttons get the clicked button on all the time
  1047. if self.Active == true then
  1048.  
  1049. if self.Color_C ~= nil then
  1050. color = self.Color_C
  1051. end
  1052.  
  1053. if self.Bitmap_C ~= nil then
  1054. bitmap = self.Bitmap_C
  1055. elseif self.Bitmap then
  1056. AXUI:SetColor("blue")
  1057. gr.setLineWidth(2)
  1058. gr.drawRectangle(self.BitmapX-2, self.BitmapY-2, self.BitmapX + self.BitmapWidth+2, self.BitmapY + self.BitmapHeight+2, false)
  1059. gr.setLineWidth(1)
  1060. end
  1061.  
  1062. end
  1063.  
  1064. if bitmap ~= nil then
  1065. if self.Monochrome then
  1066. AXUI:SetColor(color)
  1067. gr.drawMonochromeImage(bitmap, self.BitmapX, self.BitmapY, self.BitmapX + self.BitmapWidth, self.BitmapY + self.BitmapHeight)
  1068. else
  1069. gr.drawImage(bitmap, self.BitmapX, self.BitmapY, self.BitmapX + self.BitmapWidth, self.BitmapY + self.BitmapHeight) --Image gets drawn here
  1070. end
  1071. --gr.setColor(255,255,255,255)
  1072. --gr.drawString(bitmap .. ": X:" .. self.BitmapX .. ", Y:" .. self.BitmapY, self.BitmapX, self.BitmapY)
  1073. --gr.drawString(bitmap .. ": X:" .. math.floor(self.BitmapX) .. ", Y:" .. self.BitmapY .. " W:" .. math.floor(self.BitmapWidth) .. " H:" .. math.floor(self.BitmapHeight), self.BitmapX, self.BitmapY)
  1074. end
  1075.  
  1076. if self.Host ~= nil then
  1077.  
  1078. local thisObject = mn.getObjectFromSignature(self.Host)
  1079. AXUI:SetColor(color)
  1080. gr.setLineWidth(3)
  1081. gr.drawTargetingBrackets(thisObject, true, 10)
  1082. gr.setLineWidth(1)
  1083.  
  1084. end
  1085.  
  1086. if self.Text ~= nil then
  1087.  
  1088. if (self.TextWidth + 10) > 0 and (self.TextHeight - self.Offset ) > 0 then
  1089.  
  1090. AXUI:SetColor(color)
  1091. local scaling = false
  1092.  
  1093. if self.Scaled or self.ScaleFont then
  1094. scaling = true
  1095. end
  1096.  
  1097. --Do we need to increase our font size?
  1098. if self.Font then
  1099. AXUI:ChangeFont(self.Font)
  1100. else
  1101. AXUI:AutoFontScale(scaling)
  1102. end
  1103.  
  1104. self.FontHeight = gr.CurrentFont.Height
  1105.  
  1106. --local x1, x2, y1, y2 = 0, self.TextWidth + 10, self.Offset - self.FontHeight, self.TextHeight
  1107. local x1, x2, y1, y2 = 0, self.TextWidth + 10, self.Offset, self.TextHeight + 3
  1108.  
  1109. if self.Clipping then
  1110. gr.setClip(self.TextX, self.TextY, self.TextWidth + 11, self.TextHeight)
  1111. else
  1112. x1 = x1 + self.TextX
  1113. x2 = x2 + self.TextX
  1114. y1 = y1 + self.TextY
  1115. y2 = y2 + self.TextY
  1116. end
  1117.  
  1118. self.Lines = gr.drawString(self.Text, x1, y1, x2, y2) --Actual text drawing!!
  1119.  
  1120. if self.Clipping then
  1121. gr.resetClip()
  1122. end
  1123.  
  1124. --gr.drawString(gr.CurrentFont.Filename,self.TextX - 100, self.TextY)
  1125.  
  1126. if self.Lines > 2 then
  1127. local textBlockHeight = (self.Lines + 1 ) * self.FontHeight
  1128.  
  1129. if textBlockHeight > self.TextHeight and self.Offset > (self.TextHeight - textBlockHeight) then
  1130. AXUI:SetColor("red")
  1131. gr.drawString("More", (self.TextX + (self.TextWidth) / 2) - (gr.getStringWidth("More") / 2), self.TextY + self.TextHeight + 20)
  1132. end
  1133. end
  1134.  
  1135. end
  1136.  
  1137. end
  1138.  
  1139. AXUI:ResetFont()
  1140.  
  1141. return hovering
  1142.  
  1143. end
  1144.  
  1145. function AXUI:DrawList()
  1146.  
  1147. if self.List ~= nil then
  1148.  
  1149. --[[gr.drawString("ITEM OFFSET: " .. self.ItemOffset, 100, 100)
  1150. gr.drawString("#SELF.LIST: " .. #self.List)
  1151. gr.drawString("MAX LINES: " .. self.MaxLines)
  1152. gr.drawString("SELECTED ITEM: " .. self.SelectedItem)]]--
  1153.  
  1154. for i = 1, self.ItemLines do
  1155. self.Item[i]:Draw()
  1156. --gr.setColor(255,255,255,255)
  1157. --gr.drawString(i, self.Item[i].TextX - 40, self.Item[i].TextY)
  1158. end
  1159. end
  1160.  
  1161. end
  1162.  
  1163. function AXUI:DrawThumbnails()
  1164.  
  1165. if self.Buttons ~= nil then
  1166. for i = 1, #self.Buttons do
  1167. self.Buttons[i]:Draw()
  1168. end
  1169.  
  1170. if (self.Offset + (self.ThumbnailsAcross * self.ThumbnailsHigh)) < #self.Items and (#self.Items > (self.ThumbnailsAcross * self.ThumbnailsHigh)) then
  1171. AXUI:SetColor("red")
  1172. gr.drawString("More Images Available",self.X + ((self.Width/2) - (gr.getStringWidth("More Images Available")/2)),self.Y + self.Height - 50)
  1173. end
  1174.  
  1175. end
  1176.  
  1177. end
  1178.  
  1179. function AXUI:DDraw()
  1180.  
  1181. local thisObject = mn.getObjectFromSignature(self.Host)
  1182. local hovering = false
  1183.  
  1184. if thisObject.Position:getScreenCoords() ~= false then
  1185. local x, y = thisObject.Position:getScreenCoords()
  1186. local boxwidth, boxheight = gr.getStringWidth(self.Text), gr.CurrentFont.Height
  1187. self.TextX = self.TextX_0 + x - (gr.getStringWidth(self.Text)/2)
  1188. self.TextY = self.TextY_0 + y + boxheight
  1189. --self.TextY = self.Region.y2 + boxheight
  1190. self.TextWidth = gr.getStringWidth(self.Text)
  1191. self.TextHeight = gr.CurrentFont.Height
  1192. gr.setColor(0,0,0,192)
  1193. gr.drawRectangle(x-(boxwidth/2)-3, self.TextY-5, x+(boxwidth/2)+3, self.TextY+boxheight+3)
  1194. --gr.drawRectangle(self.TextX-(boxwidth/2)-3, self.TextY+7, self.TextX+(boxwidth/2)+3, self.TextY+13+boxheight)
  1195.  
  1196. self:CalculateRegion()
  1197. hovering = self:Draw()
  1198.  
  1199. return hovering
  1200. end
  1201.  
  1202. end
  1203.  
  1204. --Mouse Related Functions
  1205. function AXUI:UpdateMouseCoords()
  1206. --Seriously?
  1207.  
  1208. self.MouseX = io.getMouseX()
  1209. self.MouseY = io.getMouseY()
  1210.  
  1211. end
  1212.  
  1213. function AXUI:Clicked()
  1214.  
  1215. --Yousa clicken the buttan?! HOORAYS?!!!!
  1216. if self.Level == AXUI.Focus and self.Clickable == true then
  1217. if AXUI:IsMouseOver(self.Region) then
  1218. return true
  1219. else
  1220. return false
  1221. end
  1222. else
  1223. return false
  1224. end
  1225.  
  1226. end
  1227.  
  1228. function AXUI:IsMouseOver(region)
  1229.  
  1230. if region and region.x1 and region.x2 and region.y1 and region.y2 then
  1231.  
  1232. if AXUI.MouseX > AXUI.Screen.x and AXUI.MouseX < (AXUI.Screen.x + AXUI.Screen.w) and AXUI.MouseY > AXUI.Screen.y and AXUI.MouseY < (AXUI.Screen.y + AXUI.Screen.h) then
  1233. if AXUI.MouseX > region.x1 and AXUI.MouseY > region.y1 and AXUI.MouseX < region.x2 and AXUI.MouseY < region.y2 then
  1234. return true
  1235. else
  1236. return false
  1237. end
  1238. end
  1239.  
  1240. end
  1241.  
  1242. end
  1243.  
  1244. function AXUI:SetFocus(level)
  1245.  
  1246. self.Focus = level
  1247.  
  1248. end
  1249.  
  1250.  
  1251. AXUI:Init()
  1252.  
  1253. ]
  1254.  
  1255. $On Mouse Moved:
  1256. [
  1257. AXUI:UpdateMouseCoords()
  1258. ]
  1259.  
  1260. #End
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement