Ooops
November 30, 2020, 01:46:47 AM

Author Topic: [bb] Human Interface Lib by aCiD2 [ 1+ years ago ]  (Read 581 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Human Interface Lib by aCiD2 [ 1+ years ago ]
« on: June 29, 2017, 12:28:41 AM »
Title : Human Interface Lib
Author : aCiD2
Posted : 1+ years ago

Description : A very basic system for handling input presses. Simple enough to use - read the comments! :)

Code :
Code: BlitzBasic
  1. ; -----------------------------------------------------------------------------------------------------
  2. ;
  3. ; The NuClear Fusion Game Engine
  4. ;               Human Interface Library
  5. ;      
  6. ; -----------------------------------------------------------------------------------------------------
  7. ;
  8. ; This library is the support for input. It allows better input of devices than blitz supports because
  9. ; the hit mouse buttons, joystick buttons or keyboard kets are stored until the next HINT_Update is
  10. ; called.
  11. ;
  12. ; Usage
  13. ;       Usage of this library is simple. You first define which keys do what using the HIO_DefineAction
  14. ;       function (allows for keyboard, joystick and mouse support) and then at the end, or start of every
  15. ;       loop call 'HINT_Update.' Now, you can use HINT_CheckAction to check
  16. ;       an action.
  17. ;
  18. ; Thanks to..
  19. ;       Andreas|Blixt - Nice simple XML loading, taken from a verlet 2D demo you made.
  20. ;
  21. ; -----------------------------------------------------------------------------------------------------
  22.  
  23. Global HINT_Version$ = "1.0.00"
  24.  
  25. Const HINT_INPUTTYPE_MOUSE = 1
  26. Const HINT_INPUTTYPE_KEYBOARD = 2
  27. Const HINT_INPUTTYPE_JOYSTICK = 3
  28.  
  29. Const HINT_MODE_UP = 0
  30. Const HINT_MODE_HIT = 1
  31. Const HINT_MODE_DOWN = 2
  32. Const HINT_MODE_RELEASE = 3
  33.  
  34. Dim HINT_Keys(237)
  35.  
  36. Type HINT_Action
  37.         Field Name$
  38.         Field Key
  39.         Field DownMode
  40.         Field ActionType
  41. End Type
  42.  
  43. ; -----------------------------------------------------------------------------------------------------
  44. ; These functions are used for the human interface library
  45. ; -----------------------------------------------------------------------------------------------------
  46. Function HINT_CheckAction(Name$)
  47.  
  48.         Local Action.HINT_Action = HINT_FindAction_Name(Name)
  49.         Return ActionDownMode
  50.        
  51. End Function
  52.  
  53. Function HINT_DefineAction(Name$, ActionCode, ActionType = HINT_INPUTTYPE_KEYBOARD)
  54.        
  55.         Local Action.HINT_Action = New HINT_Action
  56.         ActionName = Name
  57.         ActionKey = ActionCode
  58.         ActionActionType = ActionType
  59.        
  60. End Function
  61.  
  62. Function HINT_FindAction_Name.HINT_Action(Name$)
  63.        
  64.         Local Action.HINT_Action
  65.        
  66.         For Action = Each HINT_Action
  67.                 If ActionName = Name Then Return Action
  68.         Next
  69.        
  70. End Function
  71.  
  72. Function HINT_Update()
  73.  
  74.         Local Action.HINT_Action
  75.  
  76.         For Action = Each HINT_Action
  77.                 Select ActionActionType
  78.                         Case HINT_INPUTTYPE_MOUSE
  79.                                 Select ActionDownMode
  80.                                         Case HINT_MODE_UP       If MouseDown(ActionKey) Then ActionDownMode = HINT_MODE_HIT
  81.                                         Case HINT_MODE_HIT      If MouseDown(ActionKey) Then ActionDownMode = HINT_MODE_DOWN Else ActionDownMode = HINT_MODE_UP
  82.                                         Case HINT_MODE_DOWN     If Not MouseDown(ActionKey) Then ActionDownMode = HINT_MODE_RELEASE
  83.                                         Case HINT_MODE_RELEASE ActionDownMode = HINT_MODE_UP
  84.                                 End Select
  85.                 End Select
  86.         Next
  87.  
  88. End Function
  89.  
  90. ; -----------------------------------------------------------------------------------------------------
  91. ; These functions are used for loading human interface data from config files (in xml format)
  92. ; -----------------------------------------------------------------------------------------------------
  93. Function HINT_XML_GetAttribute$(xmlline$,attr$)
  94.         Local pos1%,pos2%,temp$,name$,value$
  95.  
  96.         If Left(xmlline,1) <> "<" Or Right(xmlline,1) <> ">" Then Return ""
  97.         xmlline = Mid(xmlline,2,Len(xmlline) - 2)
  98.  
  99.         attr = Lower(attr)
  100.  
  101.         pos2 = Instr(xmlline," ")
  102.         Repeat
  103.                 pos1 = pos2
  104.                 pos2 = Instr(xmlline," ",pos1 + 1)
  105.  
  106.                 If pos1 > 0 And pos2 = 0
  107.                         pos2 = Len(xmlline)
  108.                         While Mid(xmlline,pos2,1) <> Chr(34)
  109.                                 If pos2 <= 1 Then Exit
  110.                                 pos2 = pos2 - 1
  111.                         Wend
  112.                         pos2 = pos2 + 1
  113.                 ElseIf pos1 = 0 And pos2 = 0
  114.                         Exit
  115.                 EndIf
  116.  
  117.                 temp = Mid(xmlline,pos1 + 1,pos2 - pos1 - 2)
  118.                 If Instr(temp,"=")
  119.                         name = Left(temp,Instr(temp,"=") - 1)
  120.                         value = Mid(temp,Instr(temp,"=") + 2)
  121.                         If Lower(name) = attr Then Return value
  122.                 EndIf
  123.         Forever
  124.  
  125.         Return ""
  126. End Function
  127.  
  128. Function HINT_XML_GetTag$(ParseString$)
  129.        
  130.         Local Pos
  131.        
  132.         ParseString = Trim(ParseString)
  133.        
  134.         ; Check its valid
  135.         If Left(ParseString, 1) <> "<" Or Right(ParseString, 1) <> ">" Then Return ""
  136.         If Len(ParseString) < 3 Then Return ""
  137.        
  138.        
  139.         ; Get where the end of the tag is.
  140.         Pos = Instr(ParseString, " ")
  141.         If Pos = 0 Or Pos > Instr(ParseString, ">") Then Pos = Instr(ParseString, ">")
  142.         If Pos = 0 Then Return ""
  143.        
  144.         ; Return the tag name
  145.         Return Lower$(Mid$(ParseString, 2, Pos - 2))
  146.        
  147. End Function
  148.  
  149. Function HINT_XML_LoadConfig(File$)
  150.        
  151.         Local FilePointer, CurrentLine$, Action.HINT_Action
  152.  
  153.         FilePointer = OpenFile(File)
  154.         If FilePointer = 0 Then DebugLog "[ Human Interface Error!!! XML Loading ] The requested file cannot be accessed, check its existance. '" + File + "'": Return False
  155.        
  156.         While Not Eof(FilePointer)
  157.        
  158.                 CurrentLine = ReadLine(FilePointer)
  159.                 DebugLog HINT_XML_GetTag(CurrentLine)
  160.                 Select HINT_XML_GetTag(CurrentLine)
  161.                         Case "keyboardkey"
  162.                                 Action = New HINT_Action
  163.                                 ActionName = HINT_XML_GetAttribute(CurrentLine, "Name")
  164.                                 ActionKey = HINT_XML_GetAttribute(CurrentLine, "Keycode")
  165.                                 ActionActionType = HINT_INPUTTYPE_KEYBOARD
  166.                                
  167.                         Case "mousebutton"
  168.                                 Action = New HINT_Action
  169.                                 ActionName = HINT_XML_GetAttribute(CurrentLine, "Name")
  170.                                 ActionKey = HINT_XML_GetAttribute(CurrentLine, "Mousebutton")
  171.                                 ActionActionType = HINT_INPUTTYPE_MOUSE
  172.                                
  173.                         Case "joystick"
  174.                                 Action = New HINT_Action
  175.                                 ActionName = HINT_XML_GetAttribute(CurrentLine, "Name")
  176.                                 ActionKey = HINT_XML_GetAttribute(CurrentLine, "Joybutton")
  177.                                 ActionActionType = HINT_INPUTTYPE_JOYSTICK
  178.                 End Select
  179.        
  180.         Wend
  181.        
  182. End Function
  183.  
  184. Function HINT_XML_SaveConfig(File$)
  185.  
  186.         Local FilePointer, CurrentLine$, Action.HINT_Action
  187.  
  188.         FilePointer = WriteFile(File)
  189.         If FilePointer = 0 Then DebugLog "[ Human Interface Error!!! XML Loading ] The requested file cannot be accessed, check its existance and write propeties. '" + File + "'": Return False
  190.        
  191.         For Action = Each HINT_Action
  192.                
  193.                 Select ActionActionType
  194.                         Case HINT_INPUTTYPE_MOUSE WriteLine FilePointer, "<MouseButton Name=" + Chr(34) + ActionName + Chr(34) + " MouseButton=" + Chr(34) + ActionKey + Chr(34) + ">"
  195.                         Case HINT_INPUTTYPE_KEYBOARD WriteLine FilePointer, "<Keyboard Name=" + Chr(34) + ActionName + Chr(34) + " Keycode" + Chr(34) + ActionKey + Chr(34) + ">"
  196.                         Case HINT_INPUTTYPE_KEYBOARD WriteLine FilePointer, "<Joystick Name=" + Chr(34) + ActionName + Chr(34) + " Joybutton" + Chr(34) + ActionKey + Chr(34) + ">"
  197.                 End Select
  198.                
  199.         Next
  200.  
  201. End Function
  202.  
  203. ; -----------------------------------------------------------------------------------------------------
  204. ; -----------------------------------------------------------------------------------------------------
  205. ; -----------------------------------------------------------------------------------------------------
  206. ; Example
  207.  
  208. SetBuffer BackBuffer()
  209. Stop
  210. HINT_XML_LoadConfig("Test.Config")
  211.  
  212. While Not KeyHit(1)
  213.        
  214.         Cls
  215.        
  216.         For a.hint_action = Each hint_action
  217.                 Text 0, y, a
  218. ame
  219.         Next
  220.        
  221.         ;Stop
  222.         HINT_Update
  223.        
  224.         ;If HINT_CheckAction("Click") = 3 Then RuntimeError ""
  225.        
  226.         Flip
  227.        
  228. Wend


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal