ui_pageswitch.lua 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. local l_gfx = love.graphics
  2. -- big and invisible container element
  3. -- used for paged interface, useless without page controller
  4. PageSwitch = {}
  5. PageSwitch.__index = PageSwitch
  6. PageSwitch.ident = "ui_pageswitch"
  7. PageSwitch.name = "PageSwitch"
  8. PageSwitch.updateable = true
  9. PageSwitch.updateAll = false -- if true, it will try to update all the pages in it, instead of only current one
  10. PageSwitch.isContainer = true
  11. function PageSwitch:new(name)
  12. local self = {}
  13. setmetatable(self,PageSwitch)
  14. self.pages = {}
  15. self.index = 0
  16. if name ~= nil then self.name = name end
  17. return self
  18. end
  19. setmetatable(PageSwitch,{__index = UIElement})
  20. function PageSwitch:draw()
  21. local c = #self.pages
  22. if c>0 then
  23. self.pages[self.index]:draw()
  24. end
  25. end
  26. function PageSwitch:update(dt)
  27. local c = #self.pages
  28. if c>0 then
  29. if self.updateAll == true then
  30. for i=1,c do
  31. self.pages[i]:draw()
  32. end
  33. else
  34. self.pages[self.index]:update(dt)
  35. end
  36. end
  37. end
  38. -- to prevent input overlap, input is handled only for current page
  39. function PageSwitch:mousemoved(x,y)
  40. local c = #self.pages
  41. if c>0 then self.pages[self.index]:mousemoved(x,y) end
  42. end
  43. function PageSwitch:mousepressed(x,y,b)
  44. local c = #self.pages
  45. if c>0 then self.pages[self.index]:mousepressed(x,y,b) end
  46. end
  47. function PageSwitch:mousereleased(x,y,b)
  48. local c = #self.pages
  49. if c>0 then self.pages[self.index]:mousereleased(x,y,b) end
  50. end
  51. function PageSwitch:keypressed(key,isrepeat)
  52. local c = #self.pages
  53. if c>0 then self.pages[self.index]:keypressed(key,isrepeat) end
  54. end
  55. function PageSwitch:keyreleased(key)
  56. local c = #self.pages
  57. if c>0 then self.pages[self.index]:keyreleased(key,isrepeat) end
  58. end
  59. function PageSwitch:wheelmoved(x,y)
  60. local c = #self.pages
  61. if c>0 then self.pages[self.index]:wheelmoved(x,y) end
  62. end
  63. function PageSwitch:setPosition(x,y)
  64. if self.isContainer == true then
  65. local dx,dy = (x or self.x) - self.x, (y or self.y) - self.y
  66. local c = #self.pages
  67. if c>0 then
  68. for i=1,c do
  69. local e = self.pages[i]
  70. e:setPosition(e.x+dx,e.y+dy)
  71. end
  72. end
  73. end
  74. self.x,self.y = x or self.x, y or self.y
  75. end
  76. function PageSwitch:addPage(pg)
  77. if type(pg) == "table" then
  78. local indx = table.getn(self.pages)+1
  79. table.insert(self.pages,pg)
  80. self.index = indx
  81. return pg
  82. elseif type(pg) == "string" or pg == nil then
  83. local indx = table.getn(self.pages)+1
  84. local page = Page:new(pg or ("Page"..indx))
  85. table.insert(self.pages,page)
  86. self.index = indx
  87. return page
  88. end
  89. end
  90. function PageSwitch:removePage(page)
  91. local c = #self.pages
  92. if c>0 then
  93. if page == nil then
  94. table.remove(self.pages,self.index)
  95. self:nextPage()
  96. else
  97. for i=1,c do
  98. if self.pages[i].name == page then
  99. table.remove(self.pages,i)
  100. self:nextPage()
  101. break
  102. end
  103. end
  104. end
  105. end
  106. end
  107. function PageSwitch:nextPage()
  108. local c = #self.pages
  109. self.index = self.index + 1
  110. if self.index>c then self.index = c end
  111. end
  112. function PageSwitch:prevPage()
  113. self.index = self.index-1
  114. if self.index <= 0 then self.index = 1 end
  115. end
  116. function PageSwitch:getItem(name)
  117. local c = #self.pages
  118. if c>0 then
  119. if page == nil then
  120. return self.pages[self.index]
  121. else
  122. for i=1,c do
  123. if self.pages[i].name == page then
  124. return self.pages[i]
  125. end
  126. end
  127. end
  128. end
  129. end
  130. -- the page is a copy of groupbox
  131. Page = {}
  132. Page.__index = Page
  133. Page.ident = "ui_page"
  134. Page.name = "Page"
  135. Page.caption = "Page"
  136. Page.showBorder = false
  137. function Page:new(name)
  138. local self = {}
  139. setmetatable(self,Page)
  140. self.items = {}
  141. self.drawList = {}
  142. self.updateList = {}
  143. self.inputList = {}
  144. if name ~= nil then self.name = name end
  145. return self
  146. end
  147. setmetatable(Page,{__index = GroupBox})
  148. -- this element controls pageswitch if linked to one. has page flip, add and remove buttons
  149. PageSwitchController = {}
  150. PageSwitchController.__index = PageSwitchController
  151. PageSwitchController.ident = "ui_pageswitchcontroller"
  152. PageSwitchController.name = "PageSwitchController"
  153. PageSwitchController.caption_xpad = 132
  154. PageSwitchController.caption_ypad = 8
  155. function PageSwitchController:new(name)
  156. local self = {}
  157. setmetatable(self,PageSwitchController)
  158. if name ~= nil then self.name = name end
  159. local bnext = Button:new("ButtonNext")
  160. bnext.caption = ">"
  161. bnext:setSize(32,32)
  162. bnext:setPosition(32,0)
  163. local bprev = Button:new("ButtonPrev")
  164. bprev.caption = "<"
  165. bprev:setSize(32,32)
  166. bprev:setPosition(0,0)
  167. local badd = Button:new("ButtonAdd")
  168. badd.caption = "+"
  169. badd:setSize(32,32)
  170. badd:setPosition(64,0)
  171. local brem = Button:new("ButtonRemove")
  172. brem.caption = "-"
  173. brem:setSize(32,32)
  174. brem:setPosition(96,0)
  175. self.items = {bprev,bnext,badd,brem,labcount}
  176. self.drawList = {bprev,bnext,badd,brem,labcount}
  177. self.updateList = {}
  178. self.inputList = {bprev,bnext,badd,brem}
  179. self.w = 128
  180. self.caption = "0/0"
  181. return self
  182. end
  183. setmetatable(PageSwitchController,{__index = GroupBox})
  184. function PageSwitchController:setPageSwitch(pgs)
  185. if pgs.ident == "ui_pageswitch" then
  186. self.pageswitch = pgs
  187. local psc = self
  188. local bp,bn,ba,br,lc = self.items[1],self.items[2],self.items[3],self.items[4],self.items[5]
  189. function bn:click(b) if b == "l" then pgs:nextPage() psc.caption = pgs.index.."/"..#pgs.pages end end
  190. function bp:click(b) if b == "l" then pgs:prevPage() psc.caption = pgs.index.."/"..#pgs.pages end end
  191. function ba:click(b) if b == "l" then pgs:addPage() psc.caption = pgs.index.."/"..#pgs.pages end end
  192. function br:click(b) if b == "l" then pgs:removePage() psc.caption = pgs.index.."/"..#pgs.pages end end
  193. end
  194. end