-
Notifications
You must be signed in to change notification settings - Fork 160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Engine,Editor: add Touch Script API #2440
Draft
ericoporto
wants to merge
1
commit into
adventuregamestudio:ags4
Choose a base branch
from
ericoporto:add-touch-api
base: ags4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
//============================================================================= | ||
// | ||
// Adventure Game Studio (AGS) | ||
// | ||
// Copyright (C) 1999-2011 Chris Jones and 2011-2024 various contributors | ||
// The full list of copyright holders can be found in the Copyright.txt | ||
// file, which is part of this source code distribution. | ||
// | ||
// The AGS source code is provided under the Artistic License 2.0. | ||
// A copy of this license can be found in the file License.txt and at | ||
// https://opensource.org/license/artistic-2-0/ | ||
// | ||
//============================================================================= | ||
|
||
#include <array> | ||
#include "ac/dynobj/scriptuserobject.h" | ||
#include "ac/dynobj/cc_dynamicarray.h" | ||
#include "device/mousew32.h" | ||
#include "touch.h" | ||
#include "ac/common.h" | ||
|
||
enum class touch_state { | ||
up, | ||
motion, | ||
down | ||
}; | ||
|
||
struct touch_point { | ||
int x; | ||
int y; | ||
bool down; | ||
}; | ||
|
||
struct tp { | ||
static const int MAX_POINTERS = 10; | ||
int point_count = 0; | ||
std::array<touch_point, MAX_POINTERS> touch_points = {}; | ||
} _tp; | ||
|
||
void on_touch_pointer(int pointer_id, Point position, touch_state state) | ||
{ | ||
_tp.point_count = std::max(pointer_id+1, _tp.point_count); | ||
_tp.touch_points[pointer_id].x = position.X; | ||
_tp.touch_points[pointer_id].y = position.Y; | ||
_tp.touch_points[pointer_id].down = state != touch_state::up; | ||
} | ||
|
||
void on_touch_pointer_down(int pointer_id, Point position) | ||
{ | ||
on_touch_pointer(pointer_id, position, touch_state::down); | ||
} | ||
|
||
void on_touch_pointer_motion(int pointer_id, Point position) | ||
{ | ||
on_touch_pointer(pointer_id, position, touch_state::motion); | ||
} | ||
|
||
void on_touch_pointer_up(int pointer_id, Point position) | ||
{ | ||
on_touch_pointer(pointer_id, position, touch_state::up); | ||
} | ||
|
||
DynObjectRef create_touchpoint(int n) | ||
{ | ||
if (n < 0 || n >= tp::MAX_POINTERS) | ||
quit("!Touch: invalid index for touchpoint"); | ||
|
||
touch_point p = _tp.touch_points[n]; | ||
return ScriptStructHelpers::CreateTouchPointRef(n, p.x, p.y, p.down); | ||
} | ||
|
||
void* Touch_GetTouchPoints() | ||
{ | ||
std::vector<DynObjectRef> objs{}; | ||
|
||
for(int i=0; i< _tp.point_count; i++){ | ||
objs.push_back(create_touchpoint(i)); | ||
} | ||
|
||
DynObjectRef arr = DynamicArrayHelpers::CreateScriptArray(std::move(objs)); | ||
return arr.Obj; | ||
} | ||
|
||
//============================================================================= | ||
// | ||
// Script API Functions | ||
// | ||
//============================================================================= | ||
|
||
#include "script/script_api.h" | ||
#include "script/script_runtime.h" | ||
|
||
// Point*[] () | ||
RuntimeScriptValue Sc_Touch_GetTouchPoints(const RuntimeScriptValue *params, int32_t param_count) | ||
{ | ||
API_SCALL_OBJ(ScriptUserObject, globalDynamicArray, Touch_GetTouchPoints); | ||
} | ||
|
||
void RegisterTouchAPI() | ||
{ | ||
ScFnRegister touch_api[] = { | ||
{ "Touch::GetTouchPoints^0", API_FN_PAIR(Touch_GetTouchPoints) }, | ||
}; | ||
|
||
ccAddExternalFunctions(touch_api); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
//============================================================================= | ||
// | ||
// Adventure Game Studio (AGS) | ||
// | ||
// Copyright (C) 1999-2011 Chris Jones and 2011-2024 various contributors | ||
// The full list of copyright holders can be found in the Copyright.txt | ||
// file, which is part of this source code distribution. | ||
// | ||
// The AGS source code is provided under the Artistic License 2.0. | ||
// A copy of this license can be found in the file License.txt and at | ||
// https://opensource.org/license/artistic-2-0/ | ||
// | ||
//============================================================================= | ||
#ifndef __AGS_EE_AC_TOUCH_H | ||
#define __AGS_EE_AC_TOUCH_H | ||
|
||
#include "util/geometry.h" | ||
|
||
void on_touch_pointer_down(int pointer_id, Point position); | ||
void on_touch_pointer_motion(int pointer_id, Point position); | ||
void on_touch_pointer_up(int pointer_id, Point position); | ||
|
||
|
||
#endif //__AGS_EE_AC_TOUCH_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a good idea to use
quit
here, for something that is neither a fatal error nor a user's fault.An assertion and returning
null
should be enough.But seeing how this function is used, I am not sure why is it needed at all.
The loop in Touch_GetTouchPoints() below could call
CreateTouchPointRef
directly instead, and limit iterations to MAX_POINTERS too.