fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
27686d7
settings-overhaul
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 ) 9 10self . COPY =self : Add ( "Button" ) 11self . COPY : Dock ( DOCK_TOP ) 12self . COPY : SetText ( "Copy" ) 13 14self . COPY . OnClick =function ( this ) 15self . m_pTextEntry : Copy () 16end 17 18self . PASTE =self : Add ( "Button" ) 19self . PASTE : Dock ( DOCK_TOP ) 20self . PASTE : SetText ( "Paste" ) 21 22self . PASTE . OnClick =function ( this ) 23self . m_pTextEntry : Paste () 24end 25end 26 27function PANEL : PerformLayout () 28self : SizeToChildren () 29end 30 31function PANEL : OnFocusChanged ( focus ) 32if not focus then 33-- Hide when clicking an option/clicking off 34self : SetVisible ( false ) 35end 36end 37 38local PANEL =class . create ( "TextEntry" , "Panel" ) 39 40PANEL : ACCESSOR ( "HoveredInput" , "m_bHoveredInput" , false ) 41 42local utf8 =require ( "extensions.utf8" ) 43 44function PANEL : TextEntry () 45self : super () -- Initialize our baseclass 46 47self . m_pContext =gui . create ( "TextEntryContext" ) 48self . m_pContext : SetTextEntry ( self ) 49 50self . m_sText ="" 51self . m_iCaretPos =0 52self . m_bSelectMode =false 53self . m_iSelectLeft =0 54self . m_iSelectRight =0 55self . m_bInsert =false 56self . m_cTextColor =color ( 0 , 0 , 0 ) 57self . m_cHighlightTextColor =color ( 0 , 150 , 255 , 100 ) 58self . m_pFont =graphics . newFont () --love.graphics.newFont("resource/fonts/VeraMono.ttf", 12) 59self . m_pMouseBeam =love . mouse . getSystemCursor ( "ibeam" ) 60self . m_tCharacterSizes ={} 61self . m_tUndoBuffer ={} 62self . m_tRedoBuffer ={} 63end 64 65function PANEL : ShowContext () 66self . m_pContext : SetVisible ( true ) 67self . m_pContext : GiveFocus () 68self . m_pContext : BringToFront () 69self . m_pContext : SetPos ( gui . getMousePosition ()) 70end 71 72function PANEL : Think ( dt ) 73if self : IsHovered () and self : IsEnabled () then 74love . mouse . setCursor ( self . m_pMouseBeam ) 75else 76love . mouse . setCursor () 77end 78end 79 80function PANEL : GetText () 81return self . m_sText 82end 83 84function PANEL : Paint ( w , h ) 85gui . skinHook ( "Paint" , "TextEntry" , self , w , h ) 86 87love . graphics . setColor ( self . m_cTextColor ) 88love . graphics . setFont ( self . m_pFont ) 89 90local height =self . m_pFont : getHeight () 91local tw =self . m_pFont : getWidth ( self . m_sText ) 92local pw =self : GetWidth () 93 94if tw > pw then 95love . graphics . ( self . m_sText , pw - tw - 6 , h / 2 - ( height / 2 )) 96else 97love . graphics . ( self . m_sText , 6 , h / 2 - ( height / 2 )) 98end 99 100if self . m_iSelectLeft < self . m_iSelectRight then 101local pre =self : GetPreSelection () 102local sel =self : GetSelection () 103local px =self . m_pFont : getWidth ( pre ) 104love . graphics . setColor ( self . m_cHighlightTextColor ) 105if tw > pw then 106love . graphics . rectangle ( "fill" , px - tw + pw - 6 , h / 2 - ( height / 2 ), self . m_pFont : getWidth ( sel ), height ) 107else 108love . graphics . rectangle ( "fill" , px + 6 , h / 2 - ( height / 2 ), self . m_pFont : getWidth ( sel ), height ) 109end 110end 111 112if not self : HasFocus () or not self : IsEnabled () then return end 113if math . floor ( love . timer . getTime () * 2 ) % 2 == 0 then return end -- Blink cursor on and off 114 115local pre =utf8 . sub ( self . m_sText , 1 , self . m_iCaretPos ) 116local caretPos =self . m_pFont : getWidth ( pre ) 117local caretChar =utf8 . sub ( self . m_sText , self . m_iCaretPos , self . m_iCaretPos ) 118 119if tw > pw then 120caretPos =caretPos - tw + pw - 6 121else 122caretPos =caretPos + 6 123end 124 125if caretChar == "" then caretChar =" " end 126 127if self . m_bInsert then 128love . graphics . rectangle ( "fill" , caretPos , h / 2 + ( height / 2 ), self . m_pFont : getWidth ( caretChar ), 1 ) 129else 130love . graphics . rectangle ( "fill" , caretPos , h / 2 - ( height / 2 ), 1 , height ) 131end 132end 133 134function PANEL : PaintOverlay ( w , h ) 135gui . skinHook ( "PaintOverlay" , "FocusPanel" , self , w , h ) 136end 137 138function PANEL : SetTextColor ( c ) 139self . m_cTextColor =c 140end 141 142function PANEL : OnMousePressed ( x , y , but ) 143if but == 2 then 144self : ShowContext () 145end 146end 147 148function PANEL : OnMouseReleased ( x , y , but ) 149 150end 151 152function PANEL : CaretGoToEnd () 153self . m_iCaretPos =utf8 . len ( self . m_sText ) 154end 155 156function PANEL : CaretGoToStart () 157self . m_iCaretPos =0 158end 159 160function PANEL : GetPreCaret ( offset ) 161return utf8 . sub ( self . m_sText , 1 , self . m_iCaretPos + ( offset or 0 )) 162end 163 164function PANEL : GetPostCaret ( offset ) 165return utf8 . sub ( self . m_sText , self . m_iCaretPos + 1 + ( offset or 0 )) 166end 167 168function PANEL : GetPreSelection () 169return utf8 . sub ( self . m_sText , 1 , self . m_iSelectLeft ) 170end 171 172function PANEL : GetSelection () 173return utf8 . sub ( self . m_sText , self . m_iSelectLeft + 1 , self . m_iSelectRight ) or "" 174end 175 176function PANEL : GetPostSelection () 177return utf8 . sub ( self . m_sText , self . m_iSelectRight + 1 ) or "" 178end 179 180function PANEL : ResetSelection () 181if not self . m_bSelectMode then return end 182self . m_iSelectLeft =0 183self . m_iSelectRight =0 184self . m_bSelectMode =false 185end 186 187function PANEL : EnterSelectMode () 188if self . m_bSelectMode then return end 189self . m_iSelectLeft =self . m_iCaretPos 190self . m_iSelectRight =self . m_iCaretPos 191self . m_bSelectMode =true 192end 193 194function PANEL : SelectAll () 195self : EnterSelectMode () 196self . m_iSelectLeft =0 197self . m_iSelectRight =utf8 . len ( self . m_sText ) 198self . m_iCaretPos =self . m_iSelectRight 199end 200 201function PANEL : SetText ( str ) 202self . m_sText =str 203self : ResetSelection () 204self : CaretGoToEnd () 205end 206 207function PANEL : OnHoveredTextInput ( text ) 208if self . m_bHoveredInput then 209self : OnTextInput ( text ) 210end 211end 212 213function PANEL : OnTextInput ( text ) 214if not self : IsEnabled () then return end 215 216for c in text : gfind ( "([%z\1-\127\194-\244][\128-\191]*)" ) do 217if not self . m_tCharacterSizes [ c ] then 218self . m_tCharacterSizes [ c ] =self . m_pFont : getWidth ( c ) 219end 220end 221 222if self . m_bSelectMode then 223self . m_sText =self : GetPreSelection () .. text .. self : GetPostSelection () 224self . m_iCaretPos =utf8 . len ( self : GetPreSelection () .. text ) 225else 226self . m_sText =self : GetPreCaret () .. text .. self : GetPostCaret ( self . m_bInsert and 1 or 0 ) 227self . m_iCaretPos =self . m_iCaretPos + utf8 . len ( text ) 228end 229self : UpdateUndoBuffer () 230self : ResetSelection () 231 232self : OnTextChanged ( self . m_sText , text ) 233end 234 235function PANEL : UpdateUndoBuffer () 236table . insert ( self . m_tUndoBuffer , self . m_sText ) 237end 238 239function PANEL : Copy () 240love . system . setClipboardText ( self : GetSelection ()) 241end 242 243function PANEL : Paste () 244self : OnTextInput ( love . system . getClipboardText ()) 245end 246 247function PANEL : OnHoveredKeyPressed ( key , isrepeat ) 248if self . m_bHoveredInput then 249self : OnKeyPressed ( key , isrepeat ) 250end 251end 252 253function PANEL : OnKeyPressed ( key , isrepeat ) 254if not self : IsEnabled () then return end 255 256local shift =love . keyboard . isDown ( "lshift" ) or love . keyboard . isDown ( "rshift" ) 257local control =love . keyboard . isDown ( "lctrl" ) or love . keyboard . isDown ( "rctrl" ) 258 259if key == "home" then 260self . m_iCaretPos =0 261elseif key == "end" then 262self . m_iCaretPos =utf8 . len ( self . m_sText ) 263elseif key == "backspace" then 264if self . m_iSelectLeft < self . m_iSelectRight then 265self . m_sText =self : GetPreSelection () .. self : GetPostSelection () 266self . m_iCaretPos =math . min ( self . m_iCaretPos , utf8 . len ( self . m_sText )) 267self : ResetSelection () 268else 269self . m_iCaretPos =math . max ( 0 , self . m_iCaretPos - 1 ) 270self . m_sText =self : GetPreCaret () .. self : GetPostCaret ( 1 ) 271end 272self : UpdateUndoBuffer () 273elseif key == "delete" then 274if self . m_iSelectLeft < self . m_iSelectRight then 275self . m_sText =self : GetPreSelection () .. self : GetPostSelection () 276self . m_iCaretPos =math . min ( self . m_iCaretPos , utf8 . len ( self . m_sText )) 277self : ResetSelection () 278else 279self . m_sText =self : GetPreCaret () .. self : GetPostCaret ( 1 ) 280end 281self : UpdateUndoBuffer () 282elseif key == "left" then 283local pos =math . max ( 0 , self . m_iCaretPos - 1 ) 284if shift then 285self : EnterSelectMode () 286if self . m_iCaretPos > 0 then 287if pos < self . m_iSelectLeft then 288self . m_iSelectLeft =pos -- move selection left 289else 290self . m_iSelectRight =pos -- deselect from the right of the selection 291end 292end 293self . m_iCaretPos =pos 294else 295if self . m_bSelectMode then 296self . m_iCaretPos =utf8 . len ( self : GetPreSelection ()) 297else 298self . m_iCaretPos =pos 299end 300self : ResetSelection () 301end 302elseif key == "right" then 303local pos =math . min ( utf8 . len ( self . m_sText ), self . m_iCaretPos + 1 ) 304if shift then 305self : EnterSelectMode () 306if self . m_iCaretPos < utf8 . len ( self . m_sText ) then 307if pos > self . m_iSelectRight then 308self . m_iSelectRight =pos -- move selection right 309else 310self . m_iSelectLeft =pos -- deselect from the left of the selection 311end 312end 313self . m_iCaretPos =pos 314else 315if self . m_bSelectMode then 316self . m_iCaretPos =utf8 . len ( self : GetPreSelection () .. self : GetSelection ()) 317else 318self . m_iCaretPos =pos 319end 320self : ResetSelection () 321end 322self . m_iCaretPos =pos 323elseif key == "insert" then 324self . m_bInsert =not self . m_bInsert 325elseif key == "up" then 326elseif key == "down" then 327elseif control then 328if key == "a" then 329self : SelectAll () 330elseif key == "x" then 331self : Copy () 332self . m_sText =self : GetPreSelection () .. self : GetPostSelection () 333self : UpdateUndoBuffer () 334self . m_iCaretPos =math . min ( self . m_iCaretPos , utf8 . len ( self . m_sText )) 335self : ResetSelection () 336elseif key == "c" then 337self : Copy () 338elseif key == "v" then 339self : Paste () 340elseif key == "y" then 341local pop =table . remove ( self . m_tRedoBuffer ) or "" 342if pop ~= "" then 343self . m_sText =pop 344self : CaretGoToEnd () 345self : ResetSelection () 346table . insert ( self . m_tUndoBuffer , pop ) 347end 348elseif key == "z" then 349local pop =table . remove ( self . m_tUndoBuffer ) or "" 350self . m_sText =self . m_tUndoBuffer [ # self . m_tUndoBuffer ] or "" 351self : CaretGoToEnd () 352self : ResetSelection () 353if pop ~= "" then 354table . insert ( self . m_tRedoBuffer , pop ) 355end 356end 357end 358end 359 360function PANEL : OnHoveredOnKeyReleased ( key ) 361if self . m_bHoveredInput then 362self : OnKeyReleased ( key ) 363end 364end 365 366function PANEL : OnKeyReleased ( key ) 367end 368 369function PANEL : OnTextChanged ( text , add ) 370 371end