About Store Forum Documentation Contact
Donations:
225$/mo



Post Reply 
Advice on improving configurable inputs code
Author Message
SamNainocard Offline
Member

Post: #1
Advice on improving configurable inputs code
With the advice from Admin and Morto, I combined to this.

Options.h
Code:
struct ControlComponent
{
private:
    UInt btn;
public:
    
    //get
    Bool getbtn(const unsigned short type=0)
    {
        switch (type)
        {
            case 0:{if(btn < 2){return InputButton(INPUT_MS, btn).on();}else{return InputButton(INPUT_KB, btn).on();}}break;
            case 1:{if(btn < 2){return InputButton(INPUT_MS, btn).pd();}else{return InputButton(INPUT_KB, btn).pd();}}break;
        }
        return false;
    }
    UInt getvalue() {return btn;}
    UInt* getvalueaddress() {return &btn;}
    inline Str getstr(){if(btn < 2){return Ms.buttonName(btn);}else{return Kb.buttonName(KB_BUTTON(btn));}}

    //set
    void setvalue(const UInt &newb) {btn=newb;}
}extern
    moveUp, moveDown, moveLeft, moveRight, jump, lookUp, lookDown, turnLeft, turnRight, atk1, atk2;

Options.cpp
Code:
void OPTIONS::inputsPrompt(Bool isLoad)
{
    UInt btn;
    if(isLoad)
    {
        if(*btnToChange <= 1){button->text=Ms.buttonName(*btnToChange);}
else{button->text=Kb.buttonName(KB_BUTTON(*btnToChange));}
    }else
    {
        if(Kb.b(KB_ESC))    {Options.inputWindow.hide();}
else
        if (Ms.b(0)){*btnToChange = 0; button->text=Ms.buttonName(0); Options.inputWindow.hide();}
else
        if (Ms.b(1)){*btnToChange = 1; button->text=Ms.buttonName(1); Options.inputWindow.hide();}
else
        {
            for(Int i = 0x00; i <= 0xDD;i++)
            {
                btn = KB_BUTTON(i);
                if (InputButton(INPUT_KB, btn).on() && Options.btnValidate(btn)){*btnToChange = btn; button->text=Kb.buttonName(KB_BUTTON(btn)); Options.inputWindow.hide(); break;}
            }
        }
    }
}

static void input_Up(Ptr)
{
    Options.inputWindow.show();
    Options.tinputChange.set(S+"Press a key to set "+ inputDesc[0] +"\nESC to cancel");
    btnToChange = moveUp.getvalueaddress();
    button = &Options.bUp;
}
static void input_Down(Ptr)
{
    Options.inputWindow.show();
    Options.tinputChange.set(S+"Press a key to set "+ inputDesc[1] +"\nESC to cancel");
    btnToChange = moveDown.getvalueaddress();
    button = &Options.bDown;
}
...

Player.cpp
Code:
input.turn.x=turnLeft.getbtn()-turnRight.getbtn();
input.turn.y=lookUp.getbtn()-lookDown.getbtn();
input.move.x=moveRight.getbtn()-moveLeft.getbtn();
And it work well in both Keyboard and Mouse.

Thank you! grin
(This post was last modified: 07-11-2011 01:16 PM by SamNainocard.)
07-08-2011 07:19 AM
Find all posts by this user Quote this message in a reply
Morto Offline
Member

Post: #2
RE: Advice on improving configurable inputs code
hi Sam,

This is just a though but, isn't a component based class for input a better solution ??

you create a component class for buttoninput, then you have an array os buttoninputcomponents(this is all the keyboard key's defined at the moment). To change the key just delete the corresponding component and create a new one for the new keyboard key.
07-08-2011 11:28 AM
Find all posts by this user Quote this message in a reply
SamNainocard Offline
Member

Post: #3
RE: Advice on improving configurable inputs code
Thanks for the reply.
I'm trying to make players can change their control scheme in-game. (forget to mention.)

I'm not quite understand about "delete the corresponding component and create a new one", can you explain more?
(This post was last modified: 07-08-2011 12:34 PM by SamNainocard.)
07-08-2011 12:32 PM
Find all posts by this user Quote this message in a reply
Morto Offline
Member

Post: #4
RE: Advice on improving configurable inputs code
hi,

this is the link for the component programming. http://gameprogrammingpatterns.com/component.html

the idea is you have 1 model class for keyboard input, component type class.
then you have 1 array of components.
each component is 1 key input
each component acts according to its key binding.

i can elaborate this during the weekend, actually i will elaborate this during the weekend to see how it comes out.

i will post some example code for W A S D key's.
07-08-2011 03:09 PM
Find all posts by this user Quote this message in a reply
Morto Offline
Member

Post: #5
RE: Advice on improving configurable inputs code
hi,

well first of all, i am new to programing so don't flame for my poor programing, the point here is the logic i think.

so here it goes.

the keyboard component class(need major improvements)
Code:
class KeyboardComponentClass
{
   KB_BUTTON button; // keyboard key
   DIR_ENUM button_function; // function that is suposed to be performed
   KeyboardComponentClass(KB_BUTTON kbb,DIR_ENUM bf){button=kbb; button_function=bf;} // can only construct with both
   KB_BUTTON getbtn() // getcurrent key based on function
   {
      return button;
   }
   // validation are missing(same key used for 2 distinct function like W used to forward and backward... can't be alloed)
   void newbtnfunction(KB_BUTTON b)
   {
      button=b;
   }
}

so with this class, you can add this line to your player class.
these are the default value, this can be read/setup from/on Gui editor on your options window.
Code:
KeyboardComponentClass forward(KB_W,DIRE_FORWARD),left(KB_A,DIRE_LEFT),backward(KB_S,DIRE_BACK),right(K​B_D,DIRE_RIGHT);

so after this you can change these lines on your player class

replace this
Code:
input.move.x=Kb.b(KB_D)-Kb.b(KB_A);
input.move.z=Kb.b(KB_W)-Kb.b(KB_S);

Code:
input.move.x=Kb.b(right.getbtn())-Kb.b(left.getbtn());
input.move.z=Kb.b(forward.getbtn())-Kb.b(backward.getbtn());

so now to change for example the forward function you call
Code:
Players[0].forward.newbtnfunction(KB_UP);
after this you UP key is the new forward key in game.

do you understand the logic ? if so from here you just need to create a new class to old 1 array of KeyboardComponentClass so that its easier to manage, or maybe no need for array not sure yet.

hope it helps.
07-09-2011 02:19 AM
Find all posts by this user Quote this message in a reply
SamNainocard Offline
Member

Post: #6
RE: Advice on improving configurable inputs code
Thanks for the example, I'll try it soon as I currently having a HDD problem.
07-09-2011 01:17 PM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #7
RE: Advice on improving configurable inputs code
take a look at "InputButton" class

if(InputButton(INPUT_KEYBOARD, KB_W).on())

InputButton ib;
ib.type=INPUT_KEYBOARD
ib.button=KB_W;
if(ib.pd())
07-09-2011 02:38 PM
Find all posts by this user Quote this message in a reply
SamNainocard Offline
Member

Post: #8
RE: Advice on improving configurable inputs code
Sorry guys, but I guess my question is abit confuse (or I'm the one who confuse.) So I'll rephrase my question.

With my code above I have done the following results, it can be save and load, and currently no known issues (except assign same button which I didn't start write condition yet.)
[Image: inputs.gif]

So, my question is, can my code be improved or should I change?

PS: @Admin, are you suggests to change those case '' to InputButton(INPUT_KEYBOARD, KB_W).on()?
PPS: I'll try to combine OPTIONS::inputsPrompt and Control_Setting::convert anyways, while waiting.

Edit: Solved, thank you.
(This post was last modified: 07-11-2011 01:16 PM by SamNainocard.)
07-09-2011 06:40 PM
Find all posts by this user Quote this message in a reply
Post Reply