File: Script.c

package info (click to toggle)
openclonk 7.0-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 145,828 kB
  • ctags: 33,094
  • sloc: cpp: 163,891; ansic: 82,846; xml: 29,876; python: 1,203; php: 767; makefile: 138; sh: 77
file content (146 lines) | stat: -rwxr-xr-x 3,677 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
	ClonkGamepadControl
	Handles the control of the Clonk using a gamepad.
	
	Is included by ClonkControl.ocd
*/

/*
	used properties:
	this library uses the 'this.control' property with properties declared in ClonkControl.ocd
	i.e.:
	this.control.mlastx
	this.control.mlasty
	this.control.current_object
	this.control.noholdingcallbacks
*/

local virtual_cursor;

/* This part of gamepad control handles only object-style menus.
Fullscreen menus are handled differently. */
func Control2Menu(int ctrl, int x, int y, int strength, bool repeat, bool release)
{

	/* all this stuff is already done on a higher layer - in playercontrol.c
	   now this is just the same for gamepad control */
	if (!PlayerHasVirtualCursor(GetOwner()))
		return true;
		
	if (!this->GetMenu()) return false;
	
	// fix pos of x and y
	var mex = this.control.mlastx+GetX()-GetMenu()->GetX();
	var mey = this.control.mlasty+GetY()-GetMenu()->GetY();
	
	// update angle for visual effect on the menu
	if (repeat)
	{
		if (ctrl == CON_UseDelayed || ctrl == CON_UseAltDelayed)
			this->GetMenu()->~UpdateCursor(mex,mey);
	}
	// click on menu
	if (release)
	{
		// select
		if (ctrl == CON_UseDelayed)
			this->GetMenu()->~OnMouseClick(mex,mey);
	}
	
	return true;
}

public func ObjectControlMovement(int plr, int ctrl, int strength, bool release)
{
	// from PlayerControl.c
	var result = inherited(plr,ctrl,strength,release,...);

	// do the following only if strength >= CON_Gamepad_Deadzone
	if(!release)
		if(strength != nil && strength < CON_Gamepad_Deadzone)
			return result;

	
	if(!virtual_cursor)
		virtual_cursor = FindObject(Find_ID(GUI_Crosshair),Find_Owner(GetOwner()));
	if(!virtual_cursor) return result;

	// change direction of virtual_cursor
	if(!release)
		virtual_cursor->Direction(ctrl);

	return result;
}

func ReinitializeControls()
{
	if(PlayerHasVirtualCursor(GetOwner()))
	{
		// if is aiming or in menu and no virtual cursor is there, create one
		if (!virtual_cursor)
			if (this.menu || this.control.current_object) // properties declared in ClonkControl.ocd
				VirtualCursor()->StartAim(this,false,this.menu);
	}
	else
	{
		// remove any virtual cursor
		if (virtual_cursor)
			virtual_cursor->RemoveObject();
	}
}

/* Virtual cursor stuff */

// get virtual cursor, if noone is there, create it
private func VirtualCursor()
{
	if (!virtual_cursor)
	{
		virtual_cursor = FindObject(Find_ID(GUI_Crosshair),Find_Owner(GetOwner()));
	}
	if (!virtual_cursor)
	{
		virtual_cursor = CreateObject(GUI_Crosshair,0,0,GetOwner());
	}
	
	return virtual_cursor;
}

// virtual cursor is visible
private func VirtualCursorAiming()
{
	if (!virtual_cursor) return false;
	return virtual_cursor->IsAiming();
}

// store pos of virtual cursor into mlastx, mlasty
public func UpdateVirtualCursorPos()
{
	this.control.mlastx = VirtualCursor()->GetX()-GetX();
	this.control.mlasty = VirtualCursor()->GetY()-GetY();
}

public func TriggerHoldingControl()
{
	// using has been commented because it must be possible to use the virtual
	// cursor aim also without a used object - for menus
	// However, I think the check for 'this.control.current_object' here is just an unecessary safeguard
	// since there is always a using-object if the clonk is aiming for a throw
	// or a use. If the clonk uses it, there will be callbacks that cancel the
	// callbacks to the virtual cursor
	// - Newton
	if (/*this.control.current_object && */!this.control.noholdingcallbacks)
	{
		var ctrl = CON_UseDelayed;
		if (this.control.alt)
			ctrl = CON_UseAltDelayed;
		ObjectControl(GetOwner(), ctrl, 0, 0, 0, true, false);
	}

}

func RemoveVirtualCursor()
{
	if (virtual_cursor)
			virtual_cursor->StopAim();
}