リバイバルオブ「へぇボタン for NDS」。2


昨年版と同等のタッチスクリーン処理を実装しました。
後は main.c を少し手直しくらいです。


オリジナルフラッシュ版のクリック動作を再現するが為だけに
ちょっとややこしい記述になってます。なってしまいました。
クリックイベントっぽいようなタッチスクリーン API があったら便利なのになーと。
言い訳ですね(笑)。


main.c

/*---------------------------------------------------------------------------------
	
	Hey-button for NDS
	
	version 0.01
	Dec 24, 2009
	
	By. REGEKATSU
	
---------------------------------------------------------------------------------*/

#include <nds/ndstypes.h>
#include <nds/interrupts.h>
#include <nds/arm9/input.h>

#include "Bg.h"
#include "Snd.h"
#include "Heybtn.h"


void main_Init(void);


//---------------------------------------------------------------------------------
void main_Init(void) {
//---------------------------------------------------------------------------------
	
	Bg_Init();
	Snd_Init();
	Heybtn_Init();
	
}

//---------------------------------------------------------------------------------
int main(void) {
//---------------------------------------------------------------------------------
	
	main_Init();
	
	while(1) {
		
		swiWaitForVBlank();
		
		scanKeys();
		
		Heybtn_Update();
		
	}
	
}

//---------------------------------------------------------------------------------


heybtn.h

/*---------------------------------------------------------------------------------
	
	heybtn module header
	
	version 0.01
	Dec 24, 2009
	
	By. REGEKATSU
	
---------------------------------------------------------------------------------*/

#ifndef _HEYBTN_H_
#define _HEYBTN_H_


#ifdef __cplusplus
extern "C" {
#endif


void Heybtn_Init(void);
void Heybtn_Update(void);


#ifdef __cplusplus
}
#endif

#endif	// _HEYBTN_H_


heybtn.c

/*---------------------------------------------------------------------------------
	
	heybtn module routine
	
	version 0.01
	Dec 24, 2009
	
	By. REGEKATSU
	
---------------------------------------------------------------------------------*/

#include <stdio.h>
#include <string.h>

#include <nds/ndstypes.h>
#include <nds/arm9/input.h>

#include "Heybtn.h"

#include "Bg.h"
#include "Snd.h"


#define COUNT_MIN 0
#define COUNT_MAX 20


typedef enum{
	HEYBTN_UP, 
	HEYBTN_NOUP, 
	HEYBTN_DOWN, 
	HEYBTN_CLEAR, 
	HEYBTN_NULL
}HEYBTN_ACT;

typedef struct{
	
	u16 act;
	u8 count;
	
	touchPosition touch;
	touchPosition touch_old;
	bool is_touch_clear;
	
}HeybtnStatus;


static HeybtnStatus st_heybtn;


bool heybtn_IsButtonUp(void);
bool heybtn_IsNoCountUp(void);
bool heybtn_IsButtonDown(void);
bool heybtn_IsCountClear(void);

void heybtn_CheckState(void);

void heybtn_ButtonUp(void);
void heybtn_ButtonDown(void);
void heybtn_CountClear(void);

void heybtn_DoAction(void);


//---------------------------------------------------------------------------------
bool heybtn_IsButtonUp(void){
//---------------------------------------------------------------------------------
	
	bool ans = false;
	
	if(keysUp() & (KEY_A | KEY_X)){
		
		ans = true;
		
	}else if(keysUp() & KEY_TOUCH){
		
		if( !( (st_heybtn.touch_old.px >= 208) && (st_heybtn.touch_old.px < 248) && 
			(st_heybtn.touch_old.py >= 176) && (st_heybtn.touch_old.py < 184) ) ){
			
			if(!st_heybtn.is_touch_clear){
				ans = true;
			}
			
		}
		
	}
	
	return ans;
	
}

//---------------------------------------------------------------------------------
bool heybtn_IsNoCountUp(void){
//---------------------------------------------------------------------------------
	
	bool ans = false;
	
	if(keysUp() & KEY_TOUCH){
		
		if( ( (st_heybtn.touch_old.px >= 208) && (st_heybtn.touch_old.px < 248) && 
			(st_heybtn.touch_old.py >= 176) && (st_heybtn.touch_old.py < 184) ) ){
			
			if(!st_heybtn.is_touch_clear){
				ans = true;
			}
			
		}
		
	}
	
	return ans;
	
}

//---------------------------------------------------------------------------------
bool heybtn_IsButtonDown(void){
//---------------------------------------------------------------------------------
	
	bool ans = false;
	
	if(keysDown() & (KEY_A | KEY_X)){
		
		ans = true;
		
	}else if(keysDown() & KEY_TOUCH){
		
		if( !( (st_heybtn.touch.px >= 208) && (st_heybtn.touch.px < 248) && 
			(st_heybtn.touch.py >= 176) && (st_heybtn.touch.py < 184) ) ){
			
			st_heybtn.is_touch_clear = false;
			
			ans = true;
			
		}else{
			
			st_heybtn.is_touch_clear = true;
			
		}
		
	}
	
	return ans;
	
}

//---------------------------------------------------------------------------------
bool heybtn_IsCountClear(void){
//---------------------------------------------------------------------------------
	
	bool ans = false;
	
	if(keysUp() & (KEY_B | KEY_Y)){
		
		ans = true;
		
	}else if(keysUp() & KEY_TOUCH){
		
		if( ( (st_heybtn.touch_old.px >= 208) && (st_heybtn.touch_old.px < 248) && 
			(st_heybtn.touch_old.py >= 176) && (st_heybtn.touch_old.py < 184) ) ){
			
			if(st_heybtn.is_touch_clear){
				ans = true;
			}
			
		}
		
	}
	
	return ans;
	
}

//---------------------------------------------------------------------------------
void heybtn_CheckState(void){
//---------------------------------------------------------------------------------
	
	if(heybtn_IsButtonUp()){
		st_heybtn.act = HEYBTN_UP;
		
	}else if(heybtn_IsNoCountUp()){
		st_heybtn.act = HEYBTN_NOUP;
		
	}else if(heybtn_IsButtonDown()){
		st_heybtn.act = HEYBTN_DOWN;
		
	}else if(heybtn_IsCountClear()){
		st_heybtn.act = HEYBTN_CLEAR;
		
	}else{
		st_heybtn.act = HEYBTN_NULL;
	}
	
}



//---------------------------------------------------------------------------------
void heybtn_ButtonUp(void){
//---------------------------------------------------------------------------------
	
	if(st_heybtn.count < COUNT_MAX){
		st_heybtn.count++;
		Bg_DrawCounter(st_heybtn.count);
	}
	
	Bg_DrawButtonUp();
	
}

//---------------------------------------------------------------------------------
void heybtn_NoCountUp(void){
//---------------------------------------------------------------------------------
	
	Bg_DrawButtonUp();
	
}

//---------------------------------------------------------------------------------
void heybtn_ButtonDown(void){
//---------------------------------------------------------------------------------
	
	if(st_heybtn.count < COUNT_MAX){
		Snd_Play();
	}
	
	Bg_DrawButtonDown();
	
}

//---------------------------------------------------------------------------------
void heybtn_CountClear(void){
//---------------------------------------------------------------------------------
	
	st_heybtn.count = COUNT_MIN;
	Bg_DrawCounter(st_heybtn.count);
	
}

//---------------------------------------------------------------------------------
void heybtn_DoAction(void){
//---------------------------------------------------------------------------------
	
	switch(st_heybtn.act){
		
	case HEYBTN_UP:
		heybtn_ButtonUp();
		break;
		
	case HEYBTN_NOUP:
		heybtn_NoCountUp();
		break;
		
	case HEYBTN_DOWN:
		heybtn_ButtonDown();
		break;
		
	case HEYBTN_CLEAR:
		heybtn_CountClear();
		break;
		
	default: //HEYBTN_NULL
		break;
		
	}
	
}



//---------------------------------------------------------------------------------
void Heybtn_Init(void){
//---------------------------------------------------------------------------------
	
	memset(&st_heybtn, 0, sizeof(st_heybtn));
	
	Bg_DrawButtonUp();
	Bg_DrawCounter(st_heybtn.count);
	
	printf("\x01b[22;26Hreset");
	
}

//---------------------------------------------------------------------------------
void Heybtn_Update(void){
//---------------------------------------------------------------------------------
	
	st_heybtn.touch_old = st_heybtn.touch;
	touchRead(&st_heybtn.touch);
	
	heybtn_CheckState();
	heybtn_DoAction();
	
}

//---------------------------------------------------------------------------------


プロジェクト一式は以下の URL からダウンロードすることができます。
ttp://page.freett.com/ntr/title/heybtn.zip