1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-11 23:54:18 +00:00

Added a new example project: ComponentTutorialExample, which includes a PDF tutorial describing the basics of what a Component is.

This commit is contained in:
jules 2015-02-23 10:25:59 +00:00
parent 74f8c9b9ef
commit 946e4dc50d
27 changed files with 9146 additions and 0 deletions

View file

@ -0,0 +1,100 @@
/*
==============================================================================
This file was auto-generated by the Introjucer!
It contains the basic startup code for a Juce application.
==============================================================================
*/
#include "../JuceLibraryCode/JuceHeader.h"
#include "MainComponent.h"
//==============================================================================
class ComponentTutorialExampleApplication : public JUCEApplication
{
public:
//==============================================================================
ComponentTutorialExampleApplication() {}
const String getApplicationName() override { return ProjectInfo::projectName; }
const String getApplicationVersion() override { return ProjectInfo::versionString; }
bool moreThanOneInstanceAllowed() override { return true; }
//==============================================================================
void initialise (const String& commandLine) override
{
// This method is where you should put your application's initialisation code..
mainWindow = new MainWindow (getApplicationName());
}
void shutdown() override
{
// Add your application's shutdown code here..
mainWindow = nullptr; // (deletes our window)
}
//==============================================================================
void systemRequestedQuit() override
{
// This is called when the app is being asked to quit: you can ignore this
// request and let the app carry on running, or call quit() to allow the app to close.
quit();
}
void anotherInstanceStarted (const String& commandLine) override
{
// When another instance of the app is launched while this one is running,
// this method is invoked, and the commandLine parameter tells you what
// the other instance's command-line arguments were.
}
//==============================================================================
/*
This class implements the desktop window that contains an instance of
our MainContentComponent class.
*/
class MainWindow : public DocumentWindow
{
public:
MainWindow (String name) : DocumentWindow (name,
Colours::lightgrey,
DocumentWindow::allButtons)
{
setUsingNativeTitleBar (true);
setContentOwned (new MainContentComponent(), true);
centreWithSize (getWidth(), getHeight());
setVisible (true);
}
void closeButtonPressed() override
{
// This is called when the user tries to close this window. Here, we'll just
// ask the app to quit when this happens, but you can change this to do
// whatever you need.
JUCEApplication::getInstance()->systemRequestedQuit();
}
/* Note: Be careful if you override any DocumentWindow methods - the base
class uses a lot of them, so by overriding you might break its functionality.
It's best to do all your work in your content component instead, but if
you really have to override any DocumentWindow methods, make sure your
subclass also calls the superclass's method.
*/
private:
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainWindow)
};
private:
ScopedPointer<MainWindow> mainWindow;
};
//==============================================================================
// This macro generates the main() routine that launches the app.
START_JUCE_APPLICATION (ComponentTutorialExampleApplication)

View file

@ -0,0 +1,35 @@
/*
==============================================================================
This file was auto-generated!
==============================================================================
*/
#include "MainComponent.h"
//==============================================================================
MainContentComponent::MainContentComponent()
: lightGrid ("lightGrid") //initialise the ToggleLightGridComponent object
{
setSize (600, 600);
// add the light grid to out main component.
addAndMakeVisible (lightGrid);
}
MainContentComponent::~MainContentComponent()
{
}
void MainContentComponent::paint (Graphics& g)
{
g.fillAll (Colour (0xff001F36));
}
void MainContentComponent::resized()
{
// set the size of the grid to fill the whole window.
lightGrid.setBounds (getLocalBounds());
}

View file

@ -0,0 +1,41 @@
/*
==============================================================================
This file was auto-generated!
==============================================================================
*/
#ifndef MAINCOMPONENT_H_INCLUDED
#define MAINCOMPONENT_H_INCLUDED
#include "../JuceLibraryCode/JuceHeader.h"
// include our component classes
#include "ToggleLightGridComponent.h"
//==============================================================================
/*
This component lives inside our window, and this is where you should put all
your controls and content.
*/
class MainContentComponent : public Component
{
public:
//==============================================================================
MainContentComponent();
~MainContentComponent();
void paint (Graphics&) override;
void resized() override;
private:
//==============================================================================
ToggleLightGridComponent lightGrid;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainContentComponent)
};
#endif // MAINCOMPONENT_H_INCLUDED

View file

@ -0,0 +1,53 @@
/*
==============================================================================
ToggleLightComponent.h
Created: 11 Feb 2015 9:56:51am
Author: Felix Faire
==============================================================================
*/
#ifndef TOGGLELIGHTCOMPONENT_H_INCLUDED
#define TOGGLELIGHTCOMPONENT_H_INCLUDED
/**
This class represents one of the individual lights in our grid.
*/
class ToggleLightComponent : public Component
{
public:
ToggleLightComponent (String name = "light")
: Component (name),
isOn (false)
{
}
void paint (Graphics& g) override
{
g.fillAll (Colours::black);
// Only shows the red ellipse when the button is on.
if (isOn)
{
g.setColour (Colours::red);
g.fillEllipse (getLocalBounds().toFloat());
}
}
void mouseEnter (const MouseEvent&) override
{
// button toggles state on mouse over.
isOn = ! isOn;
repaint();
}
private:
// member variables for the Component
bool isOn;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ToggleLightComponent)
};
#endif // TOGGLELIGHTCOMPONENT_H_INCLUDED

View file

@ -0,0 +1,67 @@
/*
==============================================================================
ToggleLightGridComponent.h
Created: 11 Feb 2015 9:57:34am
Author: Felix Faire
==============================================================================
*/
#ifndef TOGGLELIGHTGRIDCOMPONENT_H_INCLUDED
#define TOGGLELIGHTGRIDCOMPONENT_H_INCLUDED
#include "ToggleLightComponent.h"
/**
This is the parent class that holds multiple ToggleLightComponents in a grid.
*/
class ToggleLightGridComponent : public Component
{
public:
ToggleLightGridComponent (String name = "grid")
: Component (name)
{
// Adds the child light components and makes them visible
// within this component.
// (they currently rely on having a default constructor
// so they dont have to be individually initialised)
for (int i = 0; i < numX * numY; ++i)
addAndMakeVisible (toggleLights[i]);
}
void resized() override
{
// This creates a grid of rectangles to use as the bounds
// for all of our lights. The grid is defined with the
// width and height of this component.
int stepX = getWidth() / numX;
int stepY = getHeight() / numY;
for (int x = 0; x < numX; ++x)
{
for (int y = 0; y < numY; ++y)
{
// creates the rectangle (x, y, width, height)
Rectangle<int> elementBounds (x * stepX, y * stepY, stepX, stepY);
// set the size and position of the Toggle light to this rectangle.
toggleLights[x + numX * y].setBounds (elementBounds);
}
}
}
private:
// member variables for the Component
static const int numX = 20;
static const int numY = 20;
ToggleLightComponent toggleLights [numX * numY];
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ToggleLightGridComponent)
};
#endif // TOGGLELIGHTGRIDCOMPONENT_H_INCLUDED