Skip to content

Linklancien/vplayint

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

#Code #Pause
A #Module to handle player interaction, mostly for a game usage. A V module that manages the link between a key and it's action, also provide boutons.

It allows the change of the keybinds dynamically.

use v install Linklancien.playint to download this module and then import linklancien.playint in your .v file to use it. It uses the module gg.

Good to know for an easy use:

Interface:

Use a struct such as

  struct App {
     playint.Opt
     // your variables for you code
   }

This way it implements all the field needed for playint. Note that ctx &gg.Context is already a part of it, for more details see Appli interface

Keybindings:

You can init the struct Opt using Opt.init(), it will create 3 action:

  • none_fn used for key without bindings and with no effects
  • force close that will close the gg window, initialized as f4
  • option pause, that will allow the rendering of the option to change the key binds for instance, initialized as escape

To add your own key-bindings, use Opt.new_action(action fn (mut Appli), name string, base_key_code int) As showed

  • the first argument is the function you want to activate when the key is pressed
  • the second is the name that will be rendered in the pause menu
  • the last is a key to associate this action with, you can either use int(KeyCode.f4) from gg or pass -1 but players will have to manually change their key-binds Key-bindings may be saved in the future The last things to know about key-bindings is that you need to call Opt.on_event(e &gg.Event, mut app Appli) to handle the action (Most of the time it will be inside a on_event() function that you give to gg as in the example)

Caution

Be aware, azerty isn't support yet, so all key-bindings from your code will be in qwerty, but if you change inside the option it works well.

Option rendering:

If you want to change the key-bindings during the game, you need to render them using Opt.settings_render(), and to call Opt.check_buttons_options() inside a fn on_click(x f32, y f32, button gg.MouseButton, mut app App) that you give to gg as shown in the example.

Buttons:

They may be suppress from this module in futures updates

Example walk through: (Need to be reworked)

A V module that manages the link between a key and it's action, also provide boutons.

It allows the change of the keybinds dynamically.

use v install Linklancien.playint to download this module and then import linklancien.playint in your .v file to use it. It uses the module gg.

Good to know for an easy use:

  • use a struct that possesses all the fields of the Appli interface:

    pub interface Appli {
     mut:
       ctx &gg.Context
    
       // The function of the action
       actions_liste []fn (mut Appli)
    
       // The name of the action
       actions_names []string
    
       // The key to get an action from an event
       event_to_action map[int]int
    
       // The name of the event that lead to an action
       event_name_from_action [][]string
    
       // Changes,  -1 -> no change
       id_change int
    
       pause_scroll int
    
       // most likely between 0 & 1
       description_placement_proportion f32
    
       // most likely between 1 & 2
       bouton_placement_proportion f32
    
       text_cfg gx.TextCfg
    
       changing_options bool
    
       boutons_list []Bouton
     }
    

    This interface will be used to link your code and the informations the module needs.
    You can easily implement it as shown in this example of an App struct:

    struct App {
       playint.Opt
     }
    
  • you need to define your gg.Context, in your main function, with the following functions:

    init_fn:       on_init
    frame_fn:      on_frame
    event_fn:      on_event
    click_fn:      on_click
    resized_fn:    on_resized
    

    Here is an exemple of a main function:

    fn main() {
      mut app := &App{}
      app.ctx = gg.new_context(
      	fullscreen:    false
      	width:         100 * 8
      	height:        100 * 8
      	create_window: true
      	window_title:  '--'
      	user_data:     app
      	init_fn:       on_init
      	frame_fn:      on_frame
      	event_fn:      on_event
      	click_fn:      on_click
      	resized_fn:    on_resized
      	sample_count:  4
      	font_path:     playint.font_path
      )
      app.init()
      app.ctx.run()
      }
    
    • the on_init function is where you usually declare all your boutons, and the fonction that you want to be key-binded.
      Respectively by adding them in the boutons_list an array of your struct.
      If you want to add a button, all you need is at least 3 function that all take only (mut Appli) in there arguments:
    • function that is the fonction you want to call when the button is pressed.
    • is_visible that returns a bool, true if your button is visible and false if it's not.
    • is_actionnable that also returns a true if the button is actionnable and false if it's not.
      Most of the time it is the same as is_visible but with the if !changing_options{} && ....

    To key-binded a function by using the app.new_action function, you have to give in order: (function, 'function_name', -1 or int(gg.KeyCode.THE_KEY_YOU_WANT_TO_BE_ASSIGNED).

    • new_action needs to be called on your a playint.Opt struct
    • the function needs to be such as fn (mut Appli). If you want to access fields that are not in Appli, you can use if mut app is App{} or match app{App{}}, the type of app will change accordingly.
    • function_name is a string
    • the last argument is -1 if you don't key-bind your action or int(gg.KeyCode.THE_KEY_YOU_WANT_TO_BE_ASSIGNED) if you key_bind it.
    • the on_frame function is as followed:
    fn on_frame(mut app App) {
      app.ctx.begin()
      app.settings_render()
      app.boutons_draw(mut app)
      app.ctx.end()
    }
    

    app.settings_render() is use to draw the settings panel when app.changing_options is true.
    app.boutons_draw() is use to draw all the buttons on app.boutons_list.
    You juste have to call those two fonction in between app.ctx.begin() and app.ctx.end().
    You can put your code all around as you want.

    • the on_event function is simple you can base your's on the following:
    fn on_event(e &gg.Event, mut app App) {
      app.on_event(e, mut app)
    }
    

    You only need to call app.on_event(e, mut app) in it and the module will handle the interactions.
    You can use on_event to handle somme special event, for that, juste add your code after calling app.on_event(e, mut app)

    • the on_click function is here to trigger the buttons:
    fn on_click(x f32, y f32, button gg.MouseButton, mut app App) {
      app.check_boutons_options()
      app.boutons_check(mut app)
    }
    
    • the on_resized function is here to change the position of the button as your window extend or retract
    fn on_resized(e &gg.Event, mut app App) {
      size := gg.window_size()
      old_x := app.ctx.width
      old_y := app.ctx.height
      new_x := size.width
      new_y := size.height
    
      app.boutons_pos_resize(old_x, old_y, new_x, new_y)
    
      app.ctx.width = size.width
      app.ctx.height = size.height
    }
    

About

A V module that manage the link between a key and it's action, also provide boutons.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published