3 sets of button activations - UnityScript for Unity - menu

I'm trying to make a button activate another button, which then once the second button is clicked it will activate the third set of buttons. Thanks for your help in advance.
Here's the current coding in UnityScript:
var toggle : boolean;
function OnGUI()
{
toggle = GUI.Toggle(Rect(335, 80, 50, 50), toggle, "Button1", "button");
if (toggle && GUI.Button(Rect(335, 140, 50, 50), "Button2"))
{
// This is the part where I want the second button to activate and show the third set of button(s).
if (toggle && GUI.Button(Rect(335, 140, 50, 50), "Button3"))
{
// Third Button content here.
}
}
}

I would try to implement the logic of "click one button to activate the next" separatly from the unity UI. Just keep booleans of which are active and which are not(or set an int variable if its just one at each point in time) like:
bool b1Active = true;
bool b2Active = false;
bool b3Active = false;
if(b1Active && GUI.Button())
{
b1Active = false;
b2Active = true;
// do stuff here
}
if(b2Active && GUI.Button())
{
b2Active = false;
b3Active = true;
// do other stuff here
}
...
I am not sure if this is the most effective way to implement this but i think an approach in this direction could succeed (if i didnt misunderstand your question)
ps: if you dont want to show the buttons if they are not active you might try this instead:
if( b1Active)
{
if(GUI.Button())
{
b1Active = false;
b2Active = true;
// do stuff here
}
}

Related

Accelerator keys not working when modeless popup has the focus

My main CDialog sometimes displays a child modeless dialog like this:
It is only displayed if the user has configured it to automatically display.
It gets displayed via the main dialogs OnInitDialog where this function is called:
void CChristianLifeMinistryEditorDlg::DisplayAssignHistoryDialog()
{
BOOL bShowAssignHistoryDialog;
bShowAssignHistoryDialog = theApp.GetNumberSetting(_T("Options"), _T("SM_ShowAssignHist"), TRUE);
if (bShowAssignHistoryDialog)
{
m_pAssignHistoryDlg = std::make_unique<CAssignHistoryDlg>(); // .release();
if (m_pAssignHistoryDlg != nullptr)
{
m_pAssignHistoryDlg->SetAssignHistMap(&m_mapSPtrHist, &m_HistoryOriginal);
m_pAssignHistoryDlg->Create(IDD_DIALOG_ASSIGN_HISTORY, this);
m_pAssignHistoryDlg->ShowWindow(SW_SHOWNORMAL);
m_pAssignHistoryDlg->UpdateWindow();
m_pAssignHistoryDlg->EnableTree(false);
}
}
}
WhatI have noticed is that some of my main windows ACCELERATOR hotkey keys don't always work. I then realised that this is because the popup window has the focus. If i single click anywhere on the main dialog to give it focus, then my accelerator hotkeys function.
Is there any way that we can easily still allow the main editor to process it's hotkeys even though the modeless window might have focus? A standard way to cater for this?
The main window handles the accelerators like this:
BOOL CChristianLifeMinistryEditorDlg::PreTranslateMessage(MSG * pMsg)
{
if (m_hAccelTable)
{
if (::TranslateAccelerator(GetSafeHwnd(), m_hAccelTable, pMsg))
return TRUE;
}
}
And the popup modeless window also makes use of PreTranslateMessage (incase it is relevant):
BOOL CAssignHistoryDlg::PreTranslateMessage(MSG* pMsg)
{
BOOL bNoDispatch{}, bDealtWith = bDealtWith = FALSE ;
if ( (pMsg->message == WM_KEYDOWN || pMsg->message == WM_KEYUP ||
pMsg->message == WM_CHAR)
&& pMsg->wParam == VK_RETURN)
{
// Eat it.
bNoDispatch = TRUE ;
bDealtWith = TRUE ;
}
if (!bDealtWith)
bNoDispatch = CResizingDialog::PreTranslateMessage(pMsg);
return bNoDispatch ;
}
I would pass your m_hAccelTable from CChristianLifeMinistryEditorDlg to CAssignHistoryDlg and add this to the beginning of CAssignHistoryDlg::PreTranslateMessage:
if (m_hAccelTable)
{
if (::TranslateAccelerator(GetParent()->GetSafeHwnd(), m_hAccelTable, pMsg))
return TRUE;
}

Reusing a function for buttons that work independently of each other?

I'm trying to create something with P5.js that resembles an audio looper—that is, you record a snippet of audio, it plays back to you as a loop, and then you can record other snippets of audio to play together to create a song.
I figured out how to record audio, play it back as a loop, and stop the loop, but I'm not sure the best way to repeat that function so that it can be used for buttons that function independently of each other and record separate audio files, as I would like to record multiple loops.
I'm still pretty new to P5.js, so there's probably a simple way to do this—any ideas help! In general, if you have any ideas on how to achieve this project as a whole (the audio looper) I'd love to hear them.
This is my code:
let mic, recorder, soundFile, button;
let state = 0;
function setup() {
createCanvas(windowWidth, windowHeight);
background(200);
mic = new p5.AudioIn();
mic.start();
recorder = new p5.SoundRecorder();
recorder.setInput(mic);
soundFile = new p5.SoundFile();
button = createButton("record");
button.size(200, 100);
button.style("font-family", "Bodoni");
button.style("font-size", "48px");
button.position(10, 10, 10);
button.mouseClicked(loopRecord);
}
// this is the looper
function loopRecord() {
if (state === 0 && mic.enabled) {
recorder.record(soundFile);
background(255, 0, 0);
state++;
} else if (state === 1) {
recorder.stop();
background(0, 255, 0);
state++;
} else if (state === 2) {
soundFile.loop();
state++;
} else if (state === 3) {
soundFile.stop();
state++;
} else if (state === 4) {
state === 0;
}
}
You should somehow structure your data so that each button has its own state (and other parameters that should not be global, but instead unique to each loop).
Here an example using an array of track objects:
https://editor.p5js.org/RemyDekor/sketches/gM75vBYmk
I'll copy the code here as well
let mic
// this array will be filled afterwards
let tracksArray = [];
function setup() {
createCanvas(400, 400);
background(200);
// we need only one mic (it is still global)
mic = new p5.AudioIn();
mic.start();
// we call a function that creates a custom "track" object and puts it in the tracksArray (which you could use for some other things, but is currently un-used)
createTrack();
// we create a button that call the same function when we click on it
const addTrackButton = createButton("add track");
addTrackButton.mouseClicked(createTrack)
addTrackButton.position(10, 10)
}
// We now use names (strings) instead of abstract numbers to set the state of the track
function handleTrackButtonClick(trackObject) {
if (trackObject.state === "idle" && mic.enabled) {
trackObject.recorder.record(trackObject.soundFile);
background(255, 0, 0);
trackObject.state = "recording";
console.dir(trackObject.button.elt.innerText = "[recording..]");
} else if (trackObject.state === "recording") {
trackObject.recorder.stop();
background(0, 255, 0);
trackObject.state = "stopped";
console.dir(trackObject.button.elt.innerText = "play");
} else if (trackObject.state === "stopped") {
trackObject.soundFile.loop();
trackObject.state = "playing";
console.dir(trackObject.button.elt.innerText = "[playing..] stop")
} else if (trackObject.state === "playing") {
trackObject.soundFile.stop();
trackObject.state = "stopped";
console.dir(trackObject.button.elt.innerText = "[stopped..] play")
}
}
function createTrack() {
const newButton = createButton("record");
newButton.style("font-family", "Bodoni");
newButton.style("font-size", "48px");
// we do not position this button anymore, and use the "flow" of the html document
newButton.style("display", "block");
// this is the "track" object we create, and we attach the previously global parameters/states on it
const newTrackObject = {
button: newButton,
state: "idle",
recorder: new p5.SoundRecorder(),
soundFile: new p5.SoundFile()
};
newButton.mouseClicked(function() {
handleTrackButtonClick(newTrackObject)
});
newTrackObject.recorder.setInput(mic);
tracksArray.push(newTrackObject);
}

SDL How to check if window is maximized or minimized

I'm trying to make a borderless window that can be maximized and minimized, but I can't get any information on how to get the current state of the window (if it's minimized or maximized) and how to use it.
(edit) rough snippets of the code:
SDL_Rect minimize_area = {0,0,20,20};
Button minimize_window_button = Button(minimize_area);
SDL_Rect maximize_area = {0,0,20,20};
Button maximize_window_button = Button(maximize_area);
SDL_Rect close_area = {0,0,20,20};
Button close_window_button = Button(close_area);
// Program loop
while ( SDL_PollEvent( &event ) ) {
case SDL_MOUSEBUTTONDOWN:
if (event.button.button == SDL_BUTTON_LEFT) {
mouse.updateMousePosition();
if (close_window_button.mouseInDstRect(mouse.pos.x, mouse.pos.y)) running = false;
if (maximize_window_button.mouseInDstRect(mouse.pos.x, mouse.pos.y)) {
if (/* WAY TO KNOW IF THE WINDOW IS MAXIMIZED */) {
SDL_MaximizeWindow(window);
} else {
SDL_RestoreWindow(window);
}
}
if (minimize_window_button.mouseInDstRect(mouse.pos.x, mouse.pos.y)) {
if (/* WAY TO KNOW IF WINDOW IS MINIMIZED or UNMINIMIZED */) {
SDL_MinimizeWindow(window);
} else {
SDL_RestoreWindow(window);
}
}
}
SDL_Log("click!");
break;
}
SDL_GetWindowFlags(), check the SDL_WINDOW_MINIMIZED & SDL_WINDOW_MAXIMIZED bits.

How to make custom Visual C++ button?

To be precise, I don't want to use standard rectangular buttons. Is there a way to create your own buttons and incorporate them into Visual C++ application? For example, a button in a shape of a fruit, animal, random object...? How can this be achieved?
There are two common approaches to custom buttons. The first is to use the owner-draw optional style on a standard button. With this style set you receive a message (WM_DRAWITEM) when the button needs to be painted and you paint it yourself any way you like. A second approach is to draw the image on an existing window and analyze mouse messages to determine when a click is on the image. The common toolbar uses this approach: It doesn't really have button controls on it, just pictures that look like buttons. With this second approach you are not limited to clicks on rectangular areas.
define your own callback function for button
SetWindowLong(hMyButtonWnd,GWL_WNDPROC,(LONG)&MyButtonProc);
and here example of button callback
LRESULT CALLBACK MyButtonProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
if (message == WM_LBUTTONDOWN)
{
bBtnClicked = true;
bBtnDown = true;
SetCapture(hWnd);
nMouseX = LOWORD(lParam);
nMouseY = HIWORD(lParam);
InvalidateRect(hWnd,NULL,false);
UpdateWindow(hWnd);
}
else if (message == WM_LBUTTONUP)
{
bBtnClicked = false;
bBtnDown = false;
InvalidateRect(hWnd,NULL,false);
UpdateWindow(hWnd);
RECT rec;
GetClientRect(hWnd,&rec);
if (nMouseX > rec.left && nMouseY > rec.top
&& nMouseX < rec.right && nMouseY < rec.bottom)
{
MessageBox(NULL,L"Button clicked",L"Test",MB_OK);
}
ReleaseCapture();
}
else if (message == WM_MOUSEMOVE)
{
nMouseX = LOWORD(lParam);
nMouseY = HIWORD(lParam);
RECT rec;
GetClientRect(hWnd,&rec);
if (nMouseX <= rec.left || nMouseY <= rec.top
|| nMouseX >= rec.right || nMouseY >= rec.bottom) bBtnDown = false;
InvalidateRect(hWnd,NULL,false);
UpdateWindow(hWnd);
}
if (message == WM_PAINT)
{
PAINTSTRUCT ps;
HDC dc = BeginPaint(hWnd,&ps);
HDC c_dc = CreateCompatibleDC(NULL);
if (bBtnDown) SelectObject(c_dc,hBtnDown);
else SelectObject(c_dc,hBtnUp);
RECT rec;
GetClientRect(hWnd,&rec);
BitBlt(dc,0,0,rec.right,rec.bottom,c_dc,0,0,SRCCOPY);
DeleteDC(c_dc);
EndPaint(hWnd,&ps);
return 0;
}
else return CallWindowProc(DefProc,hWnd,message,wParam,lParam);
return 1;
}

YUI3 scrollView and mousewheel

I'm starting to work only with YUI3. I include component scrollView, but it did not work mousewheel event, in the options I have not found how to turn on it. I would appreciate any help.
var scrollView = new Y.ScrollView({
id: "scrollview",
srcNode: '.scrollview-item',
height: 375,
flick: {
minDistance: 10,
minVelocity: 0.3,
axis: "y"
}
});
scrollView.render();
I stumbled upon this as well, after some trial and error, I managed to get that working(note, that it is just plain scrolling, without an easing).
var DOM_MOUSE_SCROLL = 'DOMMouseScroll',
fixArgs = function(args) {
var a = Y.Array(args, 0, true), target;
if (Y.UA.gecko) {
a[0] = DOM_MOUSE_SCROLL;
// target = Y.config.win;
} else {
// target = Y.config.doc;
}
if (a.length < 3) {
// a[2] = target;
} else {
// a.splice(2, 0, target);
}
return a;
};
Y.Env.evt.plugins.mousewheel = {
on: function() {
return Y.Event._attach(fixArgs(arguments));
},
detach: function() {
return Y.Event.detach.apply(Y.Event, fixArgs(arguments));
}
};
This is the YUI mousewheel event, but it's changed a bit. The biggest issue was, that originally, either the window or document elements, which makes no sense(for example when you mousewheel over the #myelement you want that to be the returned target..)
Bellow is the code used to initialize the ScrollView and the function that handles the mousewheel event:
// ScrollView
var scrollView = new Y.ScrollView({
id: "scrollview",
srcNode: '#mycontainer',
height: 490,
flick: {
minDistance:10,
minVelocity:0.3,
axis: "y"
}
});
scrollView.render();
var content = scrollView.get("contentBox");
var scroll_modifier = 10; // 10px per Delta
var current_scroll_y, scroll_to;
content.on("mousewheel", function(e) {
// check whether this is the scrollview container
if ( e.currentTarget.hasClass('container') ) {
current_scroll_y = scrollView.get('scrollY');
scroll_to = current_scroll_y - ( scroll_modifier * e.wheelDelta );
// trying to scroll above top of the container - scroll to start
if ( scroll_to <= scrollView._minScrollY ) {
// in my case, this made the scrollbars plugin to move, but I'm quite sure it's important for other stuff as well :)
scrollView._uiDimensionsChange();
scrollView.scrollTo(0, scrollView._minScrollY);
} else if ( scroll_to >= scrollView._maxScrollY ) { // trying to scroll beneath the end of the container - scroll to end
scrollView._uiDimensionsChange();
scrollView.scrollTo(0, scrollView._maxScrollY);
} else { // otherwise just scroll to the calculated Y
scrollView._uiDimensionsChange();
scrollView.scrollTo(0, scroll_to);
};
// if we have scrollbars plugin, flash the scrollbar
if ( scrollView.scrollbars ) {
scrollView.scrollbars.flash();
};
// prevent browser default behavior on mouse scroll
e.preventDefault();
};
});
So basically that's how I managed to that, but my next challenge is to get the scrollbar work like regular scrollbar(when you drag it, the container should move correspondingly...)
Hope this helps anyone :)

Resources