fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
a8034e9
v2.1.2
1local PANEL =class . create ( "TextEntryContext" , "Panel" ) 2 3PANEL : ACCESSOR ( "TextEntry" , "m_pTextEntry" ) 4 5function PANEL : TextEntryContext () 6self : super () -- Initialize baseclass 7self : DisableScissor () 8self : SetVisible ( false ) 9self : DockPadding ( 2 , 2 , 2 , 2 ) 10 11self . COPY =self : Add ( "Button" ) 12self . COPY : Dock ( DOCK_TOP ) 13self . COPY : SetText ( "Copy" ) 14self . COPY : DockMargin ( 0 , 0 , 0 , 2 ) 15 16self . COPY . OnClick =function ( this ) 17self . m_pTextEntry : Copy () 18end 19 20self . PASTE =self : Add ( "Button" ) 21self . PASTE : Dock ( DOCK_TOP ) 22self . PASTE : SetText ( "Paste" ) 23self . PASTE : DockMargin ( 0 , 0 , 0 , 2 ) 24 25self . PASTE . OnClick =function ( this ) 26self . m_pTextEntry : Paste () 27end 28end 29 30function PANEL : PerformLayout () 31self : SizeToChildren () 32end 33 34function PANEL : OnFocusChanged ( focus ) 35if not focus then 36-- Hide when clicking an option/clicking off 37self : SetVisible ( false ) 38end 39end 40 41local PANEL =class . create ( "TextEntry" , "Panel" ) 42local utf8 =require ( "extensions.utf8" ) 43 44PANEL : ACCESSOR ( "HoveredInput" , "m_bHoveredInput" , false ) 45PANEL : ACCESSOR ( "TextHint" , "m_sTextHint" , "" ) 46 47function PANEL : TextEntry () 48self : super () -- Initialize our baseclass 49 50self . m_pContext =gui . create ( "TextEntryContext" ) 51self . m_pContext : SetTextEntry ( self ) 52 53self . m_sText ="" 54self . m_iCaretPos =0 55self . m_bSelectMode =false 56self . m_iSelectLeft =0 57self . m_iSelectRight =0 58self . m_bInsert =false 59self . m_cTextColor =color ( 0 , 0 , 0 ) 60self . m_cTextHintColor =color_grey 61self . m_cHighlightTextColor =color ( 0 , 150 , 255 , 100 ) 62self . m_pFont =graphics . newFont () --love.graphics.newFont("resource/fonts/VeraMono.ttf", 12) 63self . m_pMouseBeam =love . mouse . getSystemCursor ( "ibeam" ) 64self . m_tCharacterSizes ={} 65self . m_tUndoBuffer ={} 66self . m_tRedoBuffer ={} 67end 68 69function PANEL : ShowContext () 70self . m_pContext : SetVisible ( true ) 71self . m_pContext : GiveFocus () 72self . m_pContext : BringToFront () 73self . m_pContext : SetPos ( gui . getMousePosition ()) 74end 75 76function PANEL : Think ( dt ) 77if self : IsHovered () and self : IsEnabled () then 78love . mouse . setCursor ( self . m_pMouseBeam ) 79else 80love . mouse . setCursor () 81end 82end 83 84function PANEL : GetText () 85return self . m_sText 86end 87 88function PANEL : Paint ( w , h ) 89gui . skinHook ( "Paint" , "TextEntry" , self , w , h ) 90 91love . graphics . setColor ( self . m_cTextColor ) 92love . graphics . setFont ( self . m_pFont ) 93 94local drawText =self . m_sText 95 96if not self : HasFocus () and self . m_sText == "" then 97drawText =self . m_sTextHint 98love . graphics . setColor ( self . m_cTextHintColor ) 99end 100 101local height =self . m_pFont : getHeight () 102local tw =self . m_pFont : getWidth ( drawText ) 103local pw =self : GetWidth () 104 105if tw > pw then 106love . graphics . ( drawText , pw - tw - 6 , h / 2 - ( height / 2 )) 107else 108love . graphics . ( drawText , 6 , h / 2 - ( height / 2 )) 109end 110 111if self . m_iSelectLeft < self . m_iSelectRight then 112local pre =self : GetPreSelection () 113local sel =self : GetSelection () 114local px =self . m_pFont : getWidth ( pre ) 115love . graphics . setColor ( self . m_cHighlightTextColor ) 116if tw > pw then 117love . graphics . rectangle ( "fill" , px - tw + pw - 6 , h / 2 - ( height / 2 ), self . m_pFont : getWidth ( sel ), height ) 118else 119love . graphics . rectangle ( "fill" , px + 6 , h / 2 - ( height / 2 ), self . m_pFont : getWidth ( sel ), height ) 120end 121end 122 123if not self : HasFocus () or not self : IsEnabled () then return end 124if math . floor ( love . timer . getTime () * 2 ) % 2 == 0 then return end -- Blink cursor on and off 125 126local pre =utf8 . sub ( drawText , 1 , self . m_iCaretPos ) 127local caretPos =self . m_pFont : getWidth ( pre ) 128local caretChar =utf8 . sub ( drawText , self . m_iCaretPos , self . m_iCaretPos ) 129 130if tw > pw then 131caretPos =caretPos - tw + pw - 6 132else 133caretPos =caretPos + 6 134end 135 136if caretChar == "" then caretChar =" " end 137 138if self . m_bInsert then 139love . graphics . rectangle ( "fill" , caretPos , h / 2 + ( height / 2 ), self . m_pFont : getWidth ( caretChar ), 1 ) 140else 141love . graphics . rectangle ( "fill" , caretPos , h / 2 - ( height / 2 ), 1 , height ) 142end 143end 144 145function PANEL : PaintOverlay ( w , h ) 146gui . skinHook ( "PaintOverlay" , "FocusPanel" , self , w , h ) 147end 148 149function PANEL : SetTextColor ( c ) 150self . m_cTextColor =c 151end 152 153function PANEL : OnMousePressed ( x , y , but ) 154if but == 1 then 155self . m_iCaretPos =self : GetCaretPosFromMouse ( x , y ) 156 157if self . m_fDoubleClickTime and love . timer . getTime () <= self . m_fDoubleClickTime then 158-- Select the closest word chunk to the caret 159for left , right in utf8 . gmatch ( self . m_sText , '()%w+%s*()' ) do 160left =left - 1 161right =right - 1 162if self . m_iCaretPos >= left and self . m_iCaretPos <= right then 163self : SelectCharacters ( left , right ) 164self . m_iCaretPos =right 165return true 166end 167end 168 169-- Select the closest punctuation chunk to the caret 170for left , right in utf8 . gmatch ( self . m_sText , '()%p+%s*()' ) do 171left =left - 1 172right =right - 1 173if self . m_iCaretPos >= left and self . m_iCaretPos <= right then 174self : SelectCharacters ( left , right ) 175self . m_iCaretPos =right 176return true 177end 178end 179else 180self . m_fDoubleClickTime =love . timer . getTime () + 0.5 181self . m_bGrabbed =true 182self : ExitSelection () 183self : EnterSelectMode () 184return true 185end 186elseif but == 2 then 187self : ShowContext () 188return true 189end 190end 191 192function PANEL : OnMouseReleased ( x , y , but ) 193if but == 1 then 194if self . m_bGrabbed then 195self . m_bGrabbed =false 196self . m_iCaretPos =self : GetCaretPosFromMouse ( x , y ) 197end 198return true 199end 200end 201 202function PANEL : OnMouseMoved ( x , y , dx , dy , istouch ) 203if self . m_bGrabbed then 204local pos =self : GetCaretPosFromMouse ( x , y ) 205 206if self . m_iCaretPos ~= pos then 207self . m_iCaretPos =pos 208 209self : EnterSelectMode () 210 211if dx < 0 then 212if self . m_iCaretPos >= 0 then 213if pos < self . m_iSelectLeft then 214self . m_iSelectLeft =pos -- move selection left 215else 216self . m_iSelectRight =pos -- deselect from the right of the selection 217end 218end 219elseif dx > 0 then 220if self . m_iCaretPos <= utf8 . len ( self . m_sText ) then 221if pos > self . m_iSelectRight then 222self . m_iSelectRight =pos -- move selection right 223else 224self . m_iSelectLeft =pos -- deselect from the left of the selection 225end 226end 227end 228end 229return true 230end 231end 232 233function PANEL : CaretGoToEnd () 234self . m_iCaretPos =utf8 . len ( self . m_sText ) 235end 236 237function PANEL : CaretGoToStart () 238self . m_iCaretPos =0 239end 240 241function PANEL : GetPreCaret ( offset ) 242return utf8 . sub ( self . m_sText , 1 , self . m_iCaretPos + ( offset or 0 )) 243end 244 245function PANEL : GetPostCaret ( offset ) 246return utf8 . sub ( self . m_sText , self . m_iCaretPos + 1 + ( offset or 0 )) 247end 248 249function PANEL : GetPreSelection () 250return utf8 . sub ( self . m_sText , 1 , self . m_iSelectLeft ) 251end 252 253function PANEL : GetSelection () 254return utf8 . sub ( self . m_sText , self . m_iSelectLeft + 1 , self . m_iSelectRight ) or "" 255end 256 257function PANEL : GetPostSelection () 258return utf8 . sub ( self . m_sText , self . m_iSelectRight + 1 ) or "" 259end 260 261function PANEL : GetCaretPosFromMouse ( mx , my ) 262local posx =3 263local caret_pos =0 264local smallest_pos =0 265local smallest_diff =math . huge 266 267-- TODO: Support selection in Y axis.. 268 269local diff =math . abs ( mx - posx ) 270if diff < smallest_diff then 271smallest_diff =diff 272smallest_pos =caret_pos 273caret_pos =caret_pos + 1 274end 275 276-- "([%z\1-\127\194-\244][\128-\191]*)" 277for c in self . m_sText : gfind ( utf8 . charpattern ) do 278if not self . m_tCharacterSizes [ c ] then 279self . m_tCharacterSizes [ c ] =self . m_pFont : getWidth ( c ) 280end 281 282local width =self . m_tCharacterSizes [ c ] 283posx =posx + width 284 285local diff =math . abs ( mx - posx ) 286if diff < smallest_diff then 287smallest_diff =diff 288smallest_pos =caret_pos 289caret_pos =caret_pos + 1 290end 291end 292 293return smallest_pos 294end 295 296function PANEL : ExitSelection () 297if not self . m_bSelectMode then return end 298self . m_bSelectMode =false 299self : ResetSelection () 300end 301 302function PANEL : ResetSelection () 303self . m_iSelectLeft =0 304self . m_iSelectRight =0 305end 306 307function PANEL : EnterSelectMode () 308if self . m_bSelectMode then return end 309self . m_iSelectLeft =self . m_iCaretPos 310self . m_iSelectRight =self . m_iCaretPos 311self . m_bSelectMode =true 312end 313 314function PANEL : SelectCharacters ( left , right ) 315self . m_bSelectMode =true 316self . m_iSelectLeft =left 317self . m_iSelectRight =right 318end 319 320function PANEL : SelectAll () 321self : EnterSelectMode () 322self . m_iSelectLeft =0 323self . m_iSelectRight =utf8 . len ( self . m_sText ) 324self . m_iCaretPos =self . m_iSelectRight 325end 326 327function PANEL : SetText ( str ) 328self . m_sText =str 329self : ExitSelection () 330self : CaretGoToEnd () 331end 332 333function PANEL : ChangeText ( str ) 334self . m_sText =str 335self : OnTextChanged ( str ) 336end 337 338function PANEL : OnTextInput ( text ) 339if not self : IsEnabled () then return end 340 341for c in text : gfind ( "([%z\1-\127\194-\244][\128-\191]*)" ) do 342if not self . m_tCharacterSizes [ c ] then 343self . m_tCharacterSizes [ c ] =self . m_pFont : getWidth ( c ) 344end 345end 346 347if self . m_bSelectMode then 348self : ChangeText ( self : GetPreSelection () .. text .. self : GetPostSelection ()) 349self . m_iCaretPos =utf8 . len ( self : GetPreSelection () .. text ) 350else 351self : ChangeText ( self : GetPreCaret () .. text .. self : GetPostCaret ( self . m_bInsert and 1 or 0 )) 352self . m_iCaretPos =self . m_iCaretPos + utf8 . len ( text ) 353end 354self : UpdateUndoBuffer () 355self : ExitSelection () 356return true 357end 358 359function PANEL : UpdateUndoBuffer () 360table . insert ( self . m_tUndoBuffer , self . m_sText ) 361end 362 363function PANEL : Copy () 364love . system . setClipboardText ( self : GetSelection ()) 365end 366 367function PANEL : Paste () 368self : OnTextInput ( love . system . getClipboardText ()) 369end 370 371function PANEL : OnKeyPressed ( key , isrepeat ) 372if not self : IsEnabled () then return end 373 374local shift =love . keyboard . isDown ( "lshift" ) or love . keyboard . isDown ( "rshift" ) 375local control =love . keyboard . isDown ( "lctrl" ) or love . keyboard . isDown ( "rctrl" ) 376 377if key == "home" then 378self . m_iCaretPos =0 379elseif key == "end" then 380self . m_iCaretPos =utf8 . len ( self . m_sText ) 381elseif key == "backspace" then 382if self . m_iSelectLeft < self . m_iSelectRight then 383local preSelect =self : GetPreSelection () 384self : ChangeText ( preSelect .. self : GetPostSelection ()) 385self . m_iCaretPos =math . min ( self . m_iCaretPos , utf8 . len ( preSelect )) 386self : ExitSelection () 387else 388self . m_iCaretPos =math . max ( 0 , self . m_iCaretPos - 1 ) 389self : ChangeText ( self : GetPreCaret () .. self : GetPostCaret ( 1 )) 390end 391self : UpdateUndoBuffer () 392elseif key == "delete" then 393if self . m_iSelectLeft < self . m_iSelectRight then 394self : ChangeText ( self : GetPreSelection () .. self : GetPostSelection ()) 395self . m_iCaretPos =math . min ( self . m_iCaretPos , utf8 . len ( self . m_sText )) 396self : ExitSelection () 397else 398self : ChangeText ( self : GetPreCaret () .. self : GetPostCaret ( 1 )) 399end 400self : UpdateUndoBuffer () 401elseif key == "left" then 402local pos =math . max ( 0 , self . m_iCaretPos - 1 ) 403if shift then 404self : EnterSelectMode () 405if self . m_iCaretPos > 0 then 406if pos < self . m_iSelectLeft then 407self . m_iSelectLeft =pos -- move selection left 408else 409self . m_iSelectRight =pos -- deselect from the right of the selection 410end 411end 412self . m_iCaretPos =pos 413else 414if self . m_bSelectMode then 415self . m_iCaretPos =utf8 . len ( self : GetPreSelection ()) 416else 417self . m_iCaretPos =pos 418end 419self : ExitSelection () 420end 421elseif key == "right" then 422local pos =math . min ( utf8 . len ( self . m_sText ), self . m_iCaretPos + 1 ) 423if shift then 424self : EnterSelectMode () 425if self . m_iCaretPos < utf8 . len ( self . m_sText ) then 426if pos > self . m_iSelectRight then 427self . m_iSelectRight =pos -- move selection right 428else 429self . m_iSelectLeft =pos -- deselect from the left of the selection 430end 431end 432self . m_iCaretPos =pos 433else 434if self . m_bSelectMode then 435self . m_iCaretPos =utf8 . len ( self : GetPreSelection () .. self : GetSelection ()) 436else 437self . m_iCaretPos =pos 438end 439self : ExitSelection () 440end 441self . m_iCaretPos =pos 442elseif key == "insert" then 443self . m_bInsert =not self . m_bInsert 444elseif key == "up" then 445elseif key == "down" then 446elseif control then 447if key == "a" then 448self : SelectAll () 449elseif key == "x" then 450self : Copy () 451self : ChangeText ( self : GetPreSelection () .. self : GetPostSelection ()) 452self : UpdateUndoBuffer () 453self . m_iCaretPos =math . min ( self . m_iCaretPos , utf8 . len ( self . m_sText )) 454self : ExitSelection () 455elseif key == "c" then 456self : Copy () 457elseif key == "v" then 458self : Paste () 459elseif key == "y" then 460local pop =table . remove ( self . m_tRedoBuffer ) or "" 461if pop ~= "" then 462self : ChangeText ( pop ) 463self : CaretGoToEnd () 464self : ExitSelection () 465table . insert ( self . m_tUndoBuffer , pop ) 466end 467elseif key == "z" then 468local pop =table . remove ( self . m_tUndoBuffer ) or "" 469self : ChangeText ( self . m_tUndoBuffer [ # self . m_tUndoBuffer ] or "" ) 470self : CaretGoToEnd () 471self : ExitSelection () 472if pop ~= "" then 473table . insert ( self . m_tRedoBuffer , pop ) 474end 475end 476end 477return true 478end 479 480function PANEL : OnKeyReleased ( key ) 481if not self : IsEnabled () then return end 482return true 483end 484 485function PANEL : OnTextChanged ( text , add ) 486 487end