Gui.monkey2 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. #Import "PoolMod"
  2. #Import "data/frame.png"
  3. #Import "data/button.png"
  4. Global __atlas:Image
  5. Global __btnAtlas:Image
  6. Function GetAtlas:Image()
  7. If __atlas = Null
  8. __atlas = Image.Load("asset::frame.png")
  9. Endif
  10. Return __atlas
  11. End Function
  12. Function GetBtnAtlas:Image()
  13. If __btnAtlas = Null
  14. __btnAtlas = Image.Load("asset::button.png")
  15. Endif
  16. Return __btnAtlas
  17. End Function
  18. Class Button
  19. Field pos:PVector2D
  20. Field x:Float
  21. Field y:Float
  22. Field width:Float
  23. Field height:Float
  24. Field text:String
  25. Field offx:Float
  26. Field offy:Float
  27. Field activated:Int
  28. Field selected:Int
  29. Field oldDown:Int
  30. Field image:Image[]
  31. Field font:AngelFont
  32. Method New(font:AngelFont,img:Image[],x:Int,y:Int,str:String,p:PVector2D=Null)
  33. Self.font= font
  34. If p = Null
  35. Self.pos = New PVector2D(x,y)
  36. Self.x = 0
  37. Self.y = 0
  38. Else
  39. pos = p
  40. Self.x = x
  41. Self.y = y
  42. End If
  43. Self.text = str
  44. Self.width = img[0].Width
  45. Self.height = img[0].Height
  46. Self.offx = offx
  47. Self.offy = offy
  48. Self.image = img
  49. Self.SetText(str)
  50. End Method
  51. Method SetText:Void(str:String)
  52. text = str
  53. If text.Length > 0
  54. offx = (image[0].Width - font.TextWidth(str))/2.0
  55. offy = (image[0].Height - font.TextHeight(str))/2.0
  56. Endif
  57. End Method
  58. Method Update:Void()
  59. Local thisDown := Mouse.ButtonPressed(MouseButton.Left)
  60. If thisDown
  61. If Not oldDown And inArea()
  62. selected = True
  63. Endif
  64. Elseif oldDown
  65. If selected = True
  66. If activated = False
  67. If inArea()
  68. activated = True
  69. selected = False
  70. Else
  71. selected = False
  72. Endif
  73. Endif
  74. Elseif activated
  75. activated = False
  76. Endif
  77. Else
  78. activated = False
  79. Endif
  80. oldDown = thisDown
  81. End Method
  82. Method GetState:Int()
  83. Local state:Int = activated
  84. activated = False
  85. Return state
  86. End Method
  87. Method inArea:Int()
  88. Local tx := Mouse.X
  89. Local ty := Mouse.Y
  90. If tx < pos.x+x Return False
  91. If ty < pos.y+y Return False
  92. If tx > pos.x+x+width Return False
  93. If ty > pos.y+y+height Return False
  94. Return True
  95. End Method
  96. Method Render:Void(canvas:Canvas)
  97. Local index:Int = 0
  98. If selected Or inArea() Then index = 1
  99. canvas.Color = New Color(1,1,1)
  100. canvas.DrawImage(image[index],pos.x+x,pos.y+y)
  101. If text.Length > 0
  102. font.RenderText(canvas,text,pos.x+x+offx+index,pos.y+y+offy+index)
  103. Endif
  104. End Method
  105. End Class
  106. Class Button2
  107. Field pos:PVector2D=Null
  108. Field x:Float
  109. Field y:Float
  110. Field text:String=""
  111. Field width:Float
  112. Field height:Float
  113. Field offx:Float
  114. Field offy:Float
  115. Field image:Image[]
  116. Field selected:Int=0
  117. Field activated:Int=0
  118. Field oldDown:Int=0
  119. Method SetText:Void(tstr:String)
  120. text=tstr
  121. Local tw:Int=tstr.Length*Text.CharWidth()
  122. offx=(image[0].Width-tw)/2.0
  123. Local th:Int=image[0].Height-Text.CharHeight()
  124. offy=th/2.0
  125. End Method
  126. Method New(timg:Image[],tx:Int,ty:Int,tstr:String,tp:PVector2D=Null)
  127. If(tp=Null)
  128. pos=New PVector2D
  129. pos.x=tx
  130. pos.y=ty
  131. x=0.0
  132. y=0.0
  133. Else
  134. pos=tp
  135. x=(tx)
  136. y=(ty)
  137. Endif
  138. text=tstr
  139. width=timg[0].Width
  140. height=timg[0].Height
  141. offx=offx
  142. offy=offy
  143. image=timg
  144. SetText(tstr)
  145. End Method
  146. Method inArea:Int()
  147. Local ttx:Float=Mouse.X
  148. Local tty:Float=Mouse.Y
  149. If ttx<pos.x+x Return 0
  150. If tty<pos.y+y Return 0
  151. If ttx>pos.x+x+width Return 0
  152. If tty>pos.y+y+height Return 0
  153. Return 1
  154. End Method
  155. Method Update:Void()
  156. Local SelfDown:Int=Mouse.ButtonDown(MouseButton.Left)
  157. If SelfDown
  158. If Not(oldDown) And inArea()
  159. selected=1
  160. End
  161. Elseif oldDown
  162. If selected=1
  163. If activated=0
  164. If inArea()
  165. activated=1
  166. selected=0
  167. Else
  168. selected=0
  169. End
  170. End
  171. Else
  172. If activated activated = 0
  173. End
  174. End
  175. oldDown=SelfDown
  176. End Method
  177. Method GetState:Int()
  178. Local tstate:Int=activated
  179. activated=0
  180. Return tstate
  181. End Method
  182. Method Render:Void(canvas:Canvas)
  183. Local index:Int=0
  184. If selected index=1
  185. canvas.Color = New Color(1,1,1)
  186. canvas.DrawImage(image[index],pos.x+x,pos.y+y)
  187. Text.Draw(canvas,text,pos.x+x+offx+index,pos.y+y+offy+index)
  188. End Method
  189. End Class
  190. Class FrameAnimation
  191. Field x :Float
  192. Field y :Float
  193. Field px :Int
  194. Field py :Int
  195. Field width :Int
  196. Field height :Int
  197. Field delay :Int
  198. Field currentWidth :Int
  199. Field currentHeight :Int
  200. Field stp :Int
  201. Field time :Int
  202. Field animating :Int
  203. Field frameTopImg :Image[]
  204. Field frameBotImg :Image[]
  205. Field topImg :Image
  206. Field botImg :Image
  207. Field lrImg :Image[]
  208. Field rectImg :Image
  209. Method New(media:Media,x:Int,y:Int,width:Int,height:Int,delay:Int = 2,stp:Int = 40)
  210. Self.x = x
  211. Self.y = y
  212. Self.width = width
  213. Self.height = height
  214. Self.delay = delay
  215. Self.currentWidth = 1
  216. Self.currentHeight = 1
  217. Self.stp = stp
  218. Self.frameTopImg = media.frameTopImg
  219. Self.frameBotImg = media.frameBotImg
  220. Self.topImg = media.topImg
  221. Self.botImg = media.botImg
  222. Self.lrImg = media.lrImg
  223. Self.rectImg = media.rectImg
  224. End Method
  225. Method Init:Void()
  226. animating = True
  227. currentWidth = 1
  228. currentHeight = 1
  229. px = x+width/2
  230. py = y+height/2
  231. time = Millisecs()
  232. End Method
  233. Method Update:Int()
  234. If animating = True
  235. If Millisecs() > time+delay
  236. currentWidth += stp
  237. currentHeight += stp
  238. px -= stp/2
  239. py -= stp/2
  240. If currentWidth > width Then
  241. currentWidth = width
  242. px = x
  243. Endif
  244. If currentHeight > height Then
  245. currentHeight = height
  246. py = y
  247. Endif
  248. If currentHeight = height And currentWidth = width
  249. animating = False
  250. Endif
  251. time = Millisecs()
  252. Endif
  253. Endif
  254. Return animating
  255. End Method
  256. Method Render:Void(canvas:Canvas)
  257. Local tw:Int = 4
  258. Local th:Int = 4
  259. Local fx:Float = px-tw
  260. Local fy:Float = py-th
  261. canvas.DrawImage(frameTopImg[0],fx,fy)
  262. canvas.DrawImage(topImg,fx+tw,fy,0,currentWidth,1)
  263. canvas.DrawImage(frameTopImg[0],fx+currentWidth+tw,fy)
  264. canvas.DrawImage(lrImg[0],fx,fy+th,0,1,currentHeight)
  265. canvas.DrawImage(lrImg[1],fx+tw+currentWidth,fy+th,0,1,currentHeight)
  266. canvas.DrawImage(frameBotImg[0],fx,fy+th+currentHeight)
  267. canvas.DrawImage(botImg,fx+tw,fy+th+currentHeight,0,currentWidth,1)
  268. canvas.DrawImage(frameBotImg[1],fx+tw+currentWidth,fy+th+currentHeight)
  269. canvas.DrawImage(rectImg,px,py,0,currentWidth,currentHeight)
  270. End Method
  271. End Class
  272. Class GuiFrame
  273. Field x:Float
  274. Field y:Float
  275. Field lft:Float
  276. Field top:Float
  277. Field right:Float
  278. Field bottom:Float
  279. Field width:Float
  280. Field height:Float
  281. Field color:Color
  282. Field leftTopImg:Image
  283. Field leftBotImg:Image
  284. Field rightTopImg:Image
  285. Field rightBotImg:Image
  286. Field topImg:Image
  287. Field botImg:Image
  288. Field leftImg:Image
  289. Field rightImg:Image
  290. Field areaImg:Image
  291. Method New(x:Float,y:Float,width:Float,height:Float,color:Color = New Color(1,1,1))
  292. Self.x = x
  293. Self.y = y
  294. Self.width = width
  295. Self.height = height
  296. Self.color = color
  297. Local atlas:Image = GetAtlas()
  298. Self.leftTopImg = New Image(atlas,0, 0,16,16)
  299. Self.leftBotImg = new Image(atlas,0,16,16,16)
  300. Self.rightTopImg = new Image(atlas,16,0,16,16)
  301. Self.rightBotImg = new Image(atlas,16,16,16,16)
  302. Self.topImg = new Image(atlas,15, 0, 1,16)
  303. Self.botImg = new Image(atlas,15,16, 1,16)
  304. Self.leftImg = new Image(atlas,0,15,16, 1)
  305. Self.rightImg = new Image(atlas,16,15,16,1)
  306. Self.areaImg = new Image(atlas,16,16,1,1)
  307. Self.lft = x - 8
  308. Self.top = y - 8
  309. Self.bottom = y+height
  310. Self.right = x+width
  311. End Method
  312. Method Setxy:Void(x:Float,y:Float)
  313. Self.x = x
  314. Self.y = y
  315. Self.lft = x - 8
  316. Self.top = y - 8
  317. Self.bottom = y+height
  318. Self.right = x+width
  319. End Method
  320. Method SetArea:Void(w:Float,h:Float)
  321. width = w
  322. height = h
  323. bottom = y + height
  324. right = x + width
  325. End Method
  326. Method Render:Void(canvas:Canvas)
  327. Local oldColor:Color = canvas.Color
  328. canvas.Color = color
  329. canvas.DrawImage(leftTopImg, lft, top,0,.5,.5)
  330. canvas.DrawImage(rightTopImg, right, top,0,0.5,0.5)
  331. canvas.DrawImage(leftBotImg, lft, bottom,0,0.5,0.5)
  332. canvas.DrawImage(rightBotImg, right, bottom,0,0.5,0.5)
  333. canvas.DrawImage(topImg, x, top,0,width,.5)
  334. canvas.DrawImage(botImg, x, bottom,0,width,.5)
  335. canvas.DrawImage(leftImg,lft,y,0,.5,height)
  336. canvas.DrawImage(rightImg,right,y,0,.5,height)
  337. canvas.DrawImage(areaImg,x,y,0,width,height)
  338. canvas.Color = oldColor
  339. End Method
  340. End Class
  341. Class Enquiry
  342. Field x:Int
  343. Field y:Int
  344. Field frame:GuiFrame
  345. Field txtBox:GuiTextBox
  346. Field okBtn:Button
  347. Field cancelBtn:Button
  348. Field startBtn:Button
  349. Method New(font:AngelFont,txt:String,x:Int,y:Int,width:Int,height:Int)
  350. frame = New GuiFrame(x-5,y-5,width+5,height+5,New Color($44/255.0,$44/255.0,$AA/255.0))
  351. txtBox = New GuiTextBox(txt,x,y,width,height)
  352. Local btnImage:Image[] = LoadFrames(GetBtnAtlas(),0,62,120,62,2)
  353. okBtn = New Button(font,btnImage,x,y+height-70,"OK")
  354. cancelBtn = New Button(font,btnImage,x+130,y+height - 70,"Cancel")
  355. startBtn = New Button(font,btnImage,x+260,y+height - 70,"Restart")
  356. End Method
  357. Method Update:Void()
  358. okBtn.Update()
  359. cancelBtn.Update()
  360. startBtn.Update()
  361. End Method
  362. Method IsCanceled:Int()
  363. Return cancelBtn.GetState() = True
  364. End Method
  365. Method IsOK:Int()
  366. Return okBtn.GetState() = True
  367. End Method
  368. Method IsRestart:Int()
  369. Return startBtn.GetState() = True
  370. End Method
  371. Method Render:Void(canvas:Canvas)
  372. frame.Render(canvas)
  373. txtBox.Render(canvas)
  374. okBtn.Render(canvas)
  375. cancelBtn.Render(canvas)
  376. startBtn.Render(canvas)
  377. End Method
  378. End Class
  379. Class StringLine
  380. Field x:Int
  381. Field y:Int
  382. Field txt:String
  383. Method New(txt:String,x:Int,y:Int)
  384. Self.txt = txt
  385. Self.x = x
  386. Self.y = y
  387. End Method
  388. Method Render:Void(canvas:Canvas)
  389. RenderText(canvas,txt,x,y)
  390. End Method
  391. End Class
  392. Class TextFieldGui
  393. Field pos:PVector2D
  394. Field x:Int
  395. Field y:Int
  396. Field image:Image
  397. Field cancelBtn:Button2
  398. Field okBtn:Button2
  399. Field textField:TextField
  400. Method New(x:Int,y:Int,txt:String,p:PVector2D,blinkDelay:Int)
  401. If( Not p)
  402. Self.pos=New PVector2D()
  403. Self.pos.x=(x)
  404. Self.pos.y=(y)
  405. Self.x=0
  406. Self.y=0
  407. Else
  408. Self.pos=p
  409. Self.x=x
  410. Self.y=y
  411. End
  412. image=media.getNameImg
  413. cancelBtn=New Button2(media.smallBtnImg,50,110,"Cancel",Self.pos)
  414. okBtn=New Button2(media.smallBtnImg,200,110,"Ok",Self.pos)
  415. textField=New TextField(30.0,72.0,txt,15,Null,Self.pos,blinkDelay)
  416. End
  417. Method Update:Int()
  418. textField.Update()
  419. cancelBtn.Update()
  420. okBtn.Update()
  421. If cancelBtn.GetState()
  422. Return -1
  423. ElseIf okBtn.GetState()
  424. If textField.GetText()=""
  425. Return 1
  426. End
  427. Return 0
  428. End
  429. Return 1
  430. End
  431. Method GetText:String()
  432. Return Self.textField.GetText()
  433. End
  434. Method Render:Void(canvas:Canvas)
  435. canvas.DrawImage(Self.image,Self.pos.x+(Self.x),Self.pos.y+(Self.y),0)
  436. textField.Render(canvas)
  437. cancelBtn.Render(canvas)
  438. okBtn.Render(canvas)
  439. End
  440. End
  441. Class GuiTextBox
  442. Field yOffset:Int = 0
  443. Field slist:List<StringLine>
  444. Field align:Int
  445. Const ALIGN:Int = AngelFont.ALIGN_LEFT
  446. Const lineGap:Int = 5
  447. Method New(text:String,x:Int,y:Int,width:Int,alignment:Int = ALIGN)
  448. slist = New List<StringLine>
  449. Local thisLine:String = ""
  450. Local charOffset:Int = 0
  451. Local wordLen:Int = 0
  452. Local word:String = ""
  453. align = alignment
  454. yOffset = 0
  455. For Local i := 0 Until text.Length
  456. If y+yOffset > DEVICE_HEIGHT
  457. Return
  458. Endif
  459. Local asc:Int = text[i]
  460. Select asc
  461. Case 32 ' space
  462. wordLen = ATextWidth(word)
  463. If charOffset + wordLen > width
  464. slist.AddLast(New StringLine(thisLine,x,y+yOffset))
  465. yOffset += lineGap+AFontHeight()
  466. thisLine = ""
  467. charOffset = 0
  468. Endif
  469. charOffset += wordLen+GetChars()[32].xAdvance
  470. thisLine += word + " "
  471. word = ""
  472. Case 10' newline
  473. wordLen = ATextWidth(word)
  474. If charOffset + wordLen > width
  475. slist.AddLast(New StringLine(thisLine,x,y+yOffset))
  476. yOffset += lineGap+AFontHeight()
  477. thisLine = ""
  478. Endif
  479. thisLine += word
  480. slist.AddLast(New StringLine(thisLine,x,y+yOffset))
  481. yOffset += lineGap+AFontHeight()
  482. thisLine = ""
  483. charOffset = 0
  484. word = ""
  485. Default
  486. Local ch:Char = GetChars()[asc]
  487. word += String.FromChar(asc)
  488. End Select
  489. Next
  490. If word <> ""
  491. wordLen = ATextWidth(word)
  492. If charOffset + wordLen > width
  493. slist.AddLast(New StringLine(thisLine,x,y+yOffset))
  494. yOffset += lineGap+AFontHeight()
  495. thisLine = ""
  496. Endif
  497. thisLine += word
  498. Endif
  499. If thisLine <> ""
  500. slist.AddLast(New StringLine(thisLine,x,y+yOffset))
  501. yOffset += lineGap+AFontHeight()
  502. Endif
  503. End Method
  504. Method Render:Void(canvas:Canvas)
  505. Local node:List<StringLine>.Node = slist.FirstNode()
  506. canvas.Color = New Color(1,1,0)
  507. While node.Value
  508. node.Value.Render(canvas)
  509. node = node.Succ
  510. Wend
  511. canvas.Color = New Color(1,1,1)
  512. End
  513. End Class
  514. Class TextField
  515. Field x:Int
  516. Field y:Int
  517. Field pos:PVector2D
  518. Field text:String
  519. Field maxChars:Int
  520. Field cursorx:Int
  521. Field textImg:Image[]
  522. Field blink :Int
  523. Field delay :Int
  524. Field time :Int
  525. Method New(x:Float,y:Float,txt:String,maxChr:Int,txtImg:Image[]=Null,p:PVector2D= Null,blinkDelay:Int = 200)
  526. If p = Null
  527. pos = New PVector2D(x,y)
  528. Self.x = 0
  529. Self.y = 0
  530. Else
  531. Self.pos = p
  532. Self.x = x
  533. Self.y = y
  534. Endif
  535. Self.maxChars = maxChr
  536. If txtImg = Null
  537. Self.textImg = GetFont()
  538. Else
  539. Self.textImg = txtImg
  540. Endif
  541. Self.delay = blinkDelay
  542. Self.time = Millisecs()
  543. Self.text = txt
  544. If text.Length > maxChr text = text.Left(maxChr)
  545. cursorx = text.Length
  546. End Method
  547. Method SetFont:Void(image:Image[])
  548. textImg = image
  549. End Method
  550. Method GetPixelWidth:Int()
  551. Return (maxChars+1)*textImg[0].Width
  552. End Method
  553. Method GetText:String()
  554. Return text
  555. End Method
  556. Method Update:Void()
  557. Local chr:Int = Keyboard.GetChar()
  558. If chr>255 chr = Keyboard.TranslateKey(Cast<Key>(chr))
  559. If chr
  560. Select chr
  561. Case Key.Up
  562. Case Key.Down
  563. Case Key.Insert
  564. Case Key.Tab
  565. Case Key.Enter
  566. Case Key.Escape
  567. Case Key.Backspace
  568. Print cursorx
  569. If cursorx > 0
  570. text = text.Slice(0,cursorx-1)+text.Slice(cursorx,text.Length)
  571. Print text
  572. cursorx -= 1
  573. Endif
  574. Case Key.PageUp
  575. cursorx = 0
  576. Case Key.PageDown
  577. cursorx = text.Length
  578. Case Key.KeyEnd
  579. cursorx = text.Length
  580. Case Key.Home
  581. cursorx = 0
  582. Case Key.Left
  583. If cursorx > 0
  584. cursorx -= 1
  585. Endif
  586. Case Key.Right
  587. If cursorx < text.Length
  588. cursorx += 1
  589. Endif
  590. Default
  591. If text.Length < maxChars
  592. text = text.Slice(0,cursorx)+String.FromChar(chr)+text.Slice(cursorx,text.Length)
  593. cursorx +=1
  594. Endif
  595. End Select
  596. Endif
  597. If Millisecs() > time+delay
  598. blink = Not blink
  599. time = Millisecs()
  600. Endif
  601. End Method
  602. Method Render:Void(canvas:Canvas)
  603. Text.Draw(canvas,text,pos.x+x,pos.y+y)
  604. If blink canvas.DrawRect(pos.x+x+Text.Width(text),pos.y+y+Text.Height(),8,3)
  605. End Method
  606. End Class